
<span id="timeseries"></span><h1><span class="yiyi-st" id="yiyi-91">Time Series / Date functionality</span></h1>
        <blockquote>
        <p>原文：<a href="http://pandas.pydata.org/pandas-docs/stable/timeseries.html">http://pandas.pydata.org/pandas-docs/stable/timeseries.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<p><span class="yiyi-st" id="yiyi-92">pandas已经证明非常成功地作为用于处理时间序列数据的工具，特别是在金融数据分析空间中。</span><span class="yiyi-st" id="yiyi-93">使用NumPy <code class="docutils literal"><span class="pre">datetime64</span></code>和<code class="docutils literal"><span class="pre">timedelta64</span></code> dtypes，我们整合了大量来自其他Python库的特性，例如<code class="docutils literal"><span class="pre">scikits.timeseries</span></code>大量的用于操作时间序列数据的新功能。</span></p>
<p><span class="yiyi-st" id="yiyi-94">在处理时间序列数据时，我们将经常寻求：</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-95">生成固定频率日期和时间跨度的序列</span></li>
<li><span class="yiyi-st" id="yiyi-96">将时间序列整合或转换为特定频率</span></li>
<li><span class="yiyi-st" id="yiyi-97">基于各种非标准时间增量（例如，一年的最后一个营业日之前的5个工作日）或“前滚”日期向前或向后计算“相对”日期</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-98">pandas提供用于执行上述任务的相对紧凑和自足的工具集合。</span></p>
<p><span class="yiyi-st" id="yiyi-99">创建日期范围：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># 72 hours starting with midnight Jan 1st, 2011</span>
<span class="gp">In [1]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/1/2011&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">72</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;H&apos;</span><span class="p">)</span>

<span class="gp">In [2]: </span><span class="n">rng</span><span class="p">[:</span><span class="mi">5</span><span class="p">]</span>
<span class="gr">Out[2]: </span>
<span class="go">DatetimeIndex([&apos;2011-01-01 00:00:00&apos;, &apos;2011-01-01 01:00:00&apos;,</span>
<span class="go">               &apos;2011-01-01 02:00:00&apos;, &apos;2011-01-01 03:00:00&apos;,</span>
<span class="go">               &apos;2011-01-01 04:00:00&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;H&apos;)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-100">使用日期索引pandas对象：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [3]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">rng</span><span class="p">)),</span> <span class="n">index</span><span class="o">=</span><span class="n">rng</span><span class="p">)</span>

<span class="gp">In [4]: </span><span class="n">ts</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="gr">Out[4]: </span>
<span class="go">2011-01-01 00:00:00    0.469112</span>
<span class="go">2011-01-01 01:00:00   -0.282863</span>
<span class="go">2011-01-01 02:00:00   -1.509059</span>
<span class="go">2011-01-01 03:00:00   -1.135632</span>
<span class="go">2011-01-01 04:00:00    1.212112</span>
<span class="go">Freq: H, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-101">更改频率和填充间隙：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># to 45 minute frequency and forward fill</span>
<span class="gp">In [5]: </span><span class="n">converted</span> <span class="o">=</span> <span class="n">ts</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="s1">&apos;45Min&apos;</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&apos;pad&apos;</span><span class="p">)</span>

<span class="gp">In [6]: </span><span class="n">converted</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="gr">Out[6]: </span>
<span class="go">2011-01-01 00:00:00    0.469112</span>
<span class="go">2011-01-01 00:45:00    0.469112</span>
<span class="go">2011-01-01 01:30:00   -0.282863</span>
<span class="go">2011-01-01 02:15:00   -1.509059</span>
<span class="go">2011-01-01 03:00:00   -1.135632</span>
<span class="go">Freq: 45T, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-102">重新取样：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># Daily means</span>
<span class="gp">In [7]: </span><span class="n">ts</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;D&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[7]: </span>
<span class="go">2011-01-01   -0.319569</span>
<span class="go">2011-01-02   -0.337703</span>
<span class="go">2011-01-03    0.117258</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
<div class="section" id="overview">
<span id="timeseries-overview"></span><h2><span class="yiyi-st" id="yiyi-103">Overview</span></h2>
<p><span class="yiyi-st" id="yiyi-104">下表显示了pandas可以处理的时间相关类的类型以及如何创建它们。</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="17%">
<col width="32%">
<col width="51%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-105">类</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-106">备注</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-107">如何创建</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-108"><code class="docutils literal"><span class="pre">Timestamp</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-109">时刻数据</span></td>
<td><span class="yiyi-st" id="yiyi-110"><code class="docutils literal"><span class="pre">to_datetime</span></code>，<code class="docutils literal"><span class="pre">Timestamp</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-111"><code class="docutils literal"><span class="pre">DatetimeIndex</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-112"><code class="docutils literal"><span class="pre">Timestamp</span></code>的索引</span></td>
<td><span class="yiyi-st" id="yiyi-113"><code class="docutils literal"><span class="pre">to_datetime</span></code>，<code class="docutils literal"><span class="pre">date_range</span></code>，<code class="docutils literal"><span class="pre">DatetimeIndex</span></code></span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-114"><code class="docutils literal"><span class="pre">Period</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-115">时期数据</span></td>
<td><span class="yiyi-st" id="yiyi-116"><code class="docutils literal"><span class="pre">Period</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-117"><code class="docutils literal"><span class="pre">PeriodIndex</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-118"><code class="docutils literal"><span class="pre">Period</span></code>的索引</span></td>
<td><span class="yiyi-st" id="yiyi-119"><code class="docutils literal"><span class="pre">period_range</span></code>，<code class="docutils literal"><span class="pre">PeriodIndex</span></code></span></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="time-stamps-vs-time-spans">
<span id="timeseries-representation"></span><h2><span class="yiyi-st" id="yiyi-120">Time Stamps vs. Time Spans</span></h2>
<p><span class="yiyi-st" id="yiyi-121">时间戳数据是将值与时间点相关联的最基本类型的时间序列数据。</span><span class="yiyi-st" id="yiyi-122">对于熊猫对象，它意味着使用时间点。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [8]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="gr">Out[8]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-05-01 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [9]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-05-01&apos;</span><span class="p">)</span>
<span class="gr">Out[9]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-05-01 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [10]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[10]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-05-01 00:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-123">但是，在许多情况下，更像是将变化变量与时间范围相关联。</span><span class="yiyi-st" id="yiyi-124">由<code class="docutils literal"><span class="pre">Period</span></code>表示的范围可以明确指定，也可以从datetime字符串格式推断。</span></p>
<p><span class="yiyi-st" id="yiyi-125">例如：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [11]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2011-01&apos;</span><span class="p">)</span>
<span class="gr">Out[11]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2011-01&apos;</span><span class="p">,</span> <span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [12]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-05&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>
<span class="gr">Out[12]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-05-01&apos;</span><span class="p">,</span> <span class="s1">&apos;D&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-126"><code class="docutils literal"><span class="pre">Timestamp</span></code>和<code class="docutils literal"><span class="pre">Period</span></code>可以是索引。</span><span class="yiyi-st" id="yiyi-127"><code class="docutils literal"><span class="pre">Timestamp</span></code>和<code class="docutils literal"><span class="pre">Period</span></code>的列表将分别自动强制为<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>和<code class="docutils literal"><span class="pre">PeriodIndex</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [13]: </span><span class="n">dates</span> <span class="o">=</span> <span class="p">[</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-05-01&apos;</span><span class="p">),</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-05-02&apos;</span><span class="p">),</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-05-03&apos;</span><span class="p">)]</span>

<span class="gp">In [14]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">dates</span><span class="p">)</span>

<span class="gp">In [15]: </span><span class="nb">type</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">index</span><span class="p">)</span>
<span class="gr">Out[15]: </span><span class="n">pandas</span><span class="o">.</span><span class="n">tseries</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">DatetimeIndex</span>

<span class="gp">In [16]: </span><span class="n">ts</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[16]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2012-05-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-05-02&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-05-03&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>

<span class="gp">In [17]: </span><span class="n">ts</span>
<span class="gr">Out[17]: </span>
<span class="go">2012-05-01   -0.410001</span>
<span class="go">2012-05-02   -0.078638</span>
<span class="go">2012-05-03    0.545952</span>
<span class="go">dtype: float64</span>

<span class="gp">In [18]: </span><span class="n">periods</span> <span class="o">=</span> <span class="p">[</span><span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-01&apos;</span><span class="p">),</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-02&apos;</span><span class="p">),</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-03&apos;</span><span class="p">)]</span>

<span class="gp">In [19]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">periods</span><span class="p">)</span>

<span class="gp">In [20]: </span><span class="nb">type</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">index</span><span class="p">)</span>
<span class="gr">Out[20]: </span><span class="n">pandas</span><span class="o">.</span><span class="n">tseries</span><span class="o">.</span><span class="n">period</span><span class="o">.</span><span class="n">PeriodIndex</span>

<span class="gp">In [21]: </span><span class="n">ts</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[21]: </span><span class="n">PeriodIndex</span><span class="p">([</span><span class="s1">&apos;2012-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-02&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-03&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;period[M]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [22]: </span><span class="n">ts</span>
<span class="gr">Out[22]: </span>
<span class="go">2012-01   -1.219217</span>
<span class="go">2012-02   -1.226825</span>
<span class="go">2012-03    0.769804</span>
<span class="go">Freq: M, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-128">pandas允许你捕获表示和它们之间的转换。</span><span class="yiyi-st" id="yiyi-129">在内部，pandas表示使用<code class="docutils literal"><span class="pre">Timestamp</span></code>的实例的时间戳和使用<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>的实例的时间戳序列。</span><span class="yiyi-st" id="yiyi-130">对于常规时间范围，pandas对于标量值使用<code class="docutils literal"><span class="pre">Period</span></code>对象，对于跨度序列使用<code class="docutils literal"><span class="pre">PeriodIndex</span></code>。</span><span class="yiyi-st" id="yiyi-131">更好地支持具有任意起点和终点的不规则间隔将在未来版本中出现。</span></p>
</div>
<div class="section" id="converting-to-timestamps">
<span id="timeseries-converting"></span><h2><span class="yiyi-st" id="yiyi-132">Converting to Timestamps</span></h2>
<p><span class="yiyi-st" id="yiyi-133">要转换类似日期的对象的系列或列表对象，例如字符串，历元或混合，您可以使用<code class="docutils literal"><span class="pre">to_datetime</span></code>函数。</span><span class="yiyi-st" id="yiyi-134">当传递一个Series时，它返回一个Series（具有相同的索引），而类似列表被转换为DatetimeIndex：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [23]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">&apos;Jul 31, 2009&apos;</span><span class="p">,</span> <span class="s1">&apos;2010-01-10&apos;</span><span class="p">,</span> <span class="bp">None</span><span class="p">]))</span>
<span class="gr">Out[23]: </span>
<span class="go">0   2009-07-31</span>
<span class="go">1   2010-01-10</span>
<span class="go">2          NaT</span>
<span class="go">dtype: datetime64[ns]</span>

<span class="gp">In [24]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="s1">&apos;2005/11/23&apos;</span><span class="p">,</span> <span class="s1">&apos;2010.12.31&apos;</span><span class="p">])</span>
<span class="gr">Out[24]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2005-11-23&apos;</span><span class="p">,</span> <span class="s1">&apos;2010-12-31&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-135">如果您使用以日期开头的日期（例如欧式样式），则可以传递<code class="docutils literal"><span class="pre">dayfirst</span></code>旗标：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [25]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="s1">&apos;04-01-2012 10:00&apos;</span><span class="p">],</span> <span class="n">dayfirst</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gr">Out[25]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2012-01-04 10:00:00&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>

<span class="gp">In [26]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="s1">&apos;14-01-2012&apos;</span><span class="p">,</span> <span class="s1">&apos;01-14-2012&apos;</span><span class="p">],</span> <span class="n">dayfirst</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gr">Out[26]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2012-01-14&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-01-14&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-136">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-137">您在上面的示例中看到<code class="docutils literal"><span class="pre">dayfirst</span></code>不是严格的，因此如果无法解析日期，那么将解析为<code class="docutils literal"><span class="pre">dayfirst</span></code>为False 。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-138">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-139">指定<code class="docutils literal"><span class="pre">format</span></code>参数可能会显着加快转换速度，而在稍后的版本中，0.13.0明确指定格式字符串&apos;％Y％m％d&apos;需要更快的路径。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-140">如果将单个字符串传递到<code class="docutils literal"><span class="pre">to_datetime</span></code>，则它将返回单个<code class="docutils literal"><span class="pre">Timestamp</span></code>。</span><span class="yiyi-st" id="yiyi-141">此外，<code class="docutils literal"><span class="pre">Timestamp</span></code>可以接受字符串输入。</span><span class="yiyi-st" id="yiyi-142">请注意，<code class="docutils literal"><span class="pre">Timestamp</span></code>不接受像<code class="docutils literal"><span class="pre">dayfirst</span></code>或<code class="docutils literal"><span class="pre">format</span></code>的字符串解析选项，如果需要，请使用<code class="docutils literal"><span class="pre">to_datetime</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [27]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="s1">&apos;2010/11/12&apos;</span><span class="p">)</span>
<span class="gr">Out[27]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2010-11-12 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [28]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2010/11/12&apos;</span><span class="p">)</span>
<span class="gr">Out[28]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2010-11-12 00:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-143"><span class="versionmodified">版本0.18.1中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-144">您还可以传递整数或字符串列的<code class="docutils literal"><span class="pre">DataFrame</span></code>，以组合到<code class="docutils literal"><span class="pre">Timestamps</span></code>的<code class="docutils literal"><span class="pre">Series</span></code>中。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [29]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;year&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">2016</span><span class="p">],</span>
<span class="gp">   ....:</span>                    <span class="s1">&apos;month&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span>
<span class="gp">   ....:</span>                    <span class="s1">&apos;day&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span>
<span class="gp">   ....:</span>                    <span class="s1">&apos;hour&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]})</span>
<span class="gp">   ....:</span> 

<span class="gp">In [30]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
<span class="gr">Out[30]: </span>
<span class="go">0   2015-02-04 02:00:00</span>
<span class="go">1   2016-03-05 03:00:00</span>
<span class="go">dtype: datetime64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-145">您只能传递需要汇编的列。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [31]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">df</span><span class="p">[[</span><span class="s1">&apos;year&apos;</span><span class="p">,</span> <span class="s1">&apos;month&apos;</span><span class="p">,</span> <span class="s1">&apos;day&apos;</span><span class="p">]])</span>
<span class="gr">Out[31]: </span>
<span class="go">0   2015-02-04</span>
<span class="go">1   2016-03-05</span>
<span class="go">dtype: datetime64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-146"><code class="docutils literal"><span class="pre">pd.to_datetime</span></code>在列名称中查找datetime组件的标准名称，包括：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-147">必需：<code class="docutils literal"><span class="pre">year</span></code>，<code class="docutils literal"><span class="pre">month</span></code>，<code class="docutils literal"><span class="pre">day</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-148">optional: <code class="docutils literal"><span class="pre">hour</span></code>, <code class="docutils literal"><span class="pre">minute</span></code>, <code class="docutils literal"><span class="pre">second</span></code>, <code class="docutils literal"><span class="pre">millisecond</span></code>, <code class="docutils literal"><span class="pre">microsecond</span></code>, <code class="docutils literal"><span class="pre">nanosecond</span></code></span></li>
</ul>
<div class="section" id="invalid-data">
<h3><span class="yiyi-st" id="yiyi-149">Invalid Data</span></h3>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-150">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-151">在版本0.17.0中，<code class="docutils literal"><span class="pre">to_datetime</span></code>的默认值现在为<code class="docutils literal"><span class="pre">errors=&apos;raise&apos;</span></code>，而不是<code class="docutils literal"><span class="pre">errors=&apos;ignore&apos;</span></code>。</span><span class="yiyi-st" id="yiyi-152">这意味着无效的解析将提升，而返回原始输入，如在以前的版本。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-153">通过<code class="docutils literal"><span class="pre">errors=&apos;coerce&apos;</span></code>将无效数据转换为<code class="docutils literal"><span class="pre">NaT</span></code>（不是时间）：</span></p>
<p><span class="yiyi-st" id="yiyi-154">当不可解析时报错，这是默认值</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [2]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="s1">&apos;2009/07/31&apos;</span><span class="p">,</span> <span class="s1">&apos;asd&apos;</span><span class="p">],</span> <span class="n">errors</span><span class="o">=</span><span class="s1">&apos;raise&apos;</span><span class="p">)</span>
<span class="go">ValueError: Unknown string format</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-155">不可解析时返回原始输入</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="s1">&apos;2009/07/31&apos;</span><span class="p">,</span> <span class="s1">&apos;asd&apos;</span><span class="p">],</span> <span class="n">errors</span><span class="o">=</span><span class="s1">&apos;ignore&apos;</span><span class="p">)</span>
<span class="gr">Out[4]: </span><span class="n">array</span><span class="p">([</span><span class="s1">&apos;2009/07/31&apos;</span><span class="p">,</span> <span class="s1">&apos;asd&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">object</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-156">不可扩展时返回NaT输入</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [6]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="s1">&apos;2009/07/31&apos;</span><span class="p">,</span> <span class="s1">&apos;asd&apos;</span><span class="p">],</span> <span class="n">errors</span><span class="o">=</span><span class="s1">&apos;coerce&apos;</span><span class="p">)</span>
<span class="gr">Out[6]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2009-07-31&apos;</span><span class="p">,</span> <span class="s1">&apos;NaT&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="epoch-timestamps">
<h3><span class="yiyi-st" id="yiyi-157">Epoch Timestamps</span></h3>
<p><span class="yiyi-st" id="yiyi-158">它也可以转换整数或浮点时代。</span><span class="yiyi-st" id="yiyi-159">这些的默认单位是纳秒（因为这些是如何存储<code class="docutils literal"><span class="pre">Timestamp</span></code>的）。</span><span class="yiyi-st" id="yiyi-160">然而，经常将时期存储在另一个可以指定的<code class="docutils literal"><span class="pre">unit</span></code>中：</span></p>
<p><span class="yiyi-st" id="yiyi-161">典型的时期存储单位</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [32]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="mi">1349720105</span><span class="p">,</span> <span class="mi">1349806505</span><span class="p">,</span> <span class="mi">1349892905</span><span class="p">,</span>
<span class="gp">   ....:</span>                 <span class="mi">1349979305</span><span class="p">,</span> <span class="mi">1350065705</span><span class="p">],</span> <span class="n">unit</span><span class="o">=</span><span class="s1">&apos;s&apos;</span><span class="p">)</span>
<span class="gp">   ....:</span> 
<span class="gr">Out[32]: </span>
<span class="go">DatetimeIndex([&apos;2012-10-08 18:15:05&apos;, &apos;2012-10-09 18:15:05&apos;,</span>
<span class="go">               &apos;2012-10-10 18:15:05&apos;, &apos;2012-10-11 18:15:05&apos;,</span>
<span class="go">               &apos;2012-10-12 18:15:05&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=None)</span>

<span class="gp">In [33]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="mi">1349720105100</span><span class="p">,</span> <span class="mi">1349720105200</span><span class="p">,</span> <span class="mi">1349720105300</span><span class="p">,</span>
<span class="gp">   ....:</span>                 <span class="mi">1349720105400</span><span class="p">,</span> <span class="mi">1349720105500</span> <span class="p">],</span> <span class="n">unit</span><span class="o">=</span><span class="s1">&apos;ms&apos;</span><span class="p">)</span>
<span class="gp">   ....:</span> 
<span class="gr">Out[33]: </span>
<span class="go">DatetimeIndex([&apos;2012-10-08 18:15:05.100000&apos;, &apos;2012-10-08 18:15:05.200000&apos;,</span>
<span class="go">               &apos;2012-10-08 18:15:05.300000&apos;, &apos;2012-10-08 18:15:05.400000&apos;,</span>
<span class="go">               &apos;2012-10-08 18:15:05.500000&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=None)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-162">这些<em>工作</em>，但结果可能是意外的。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [34]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="mi">1</span><span class="p">])</span>
<span class="gr">Out[34]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;1970-01-01 00:00:00.000000001&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>

<span class="gp">In [35]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mf">3.14</span><span class="p">],</span> <span class="n">unit</span><span class="o">=</span><span class="s1">&apos;s&apos;</span><span class="p">)</span>
<span class="gr">Out[35]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;1970-01-01 00:00:01&apos;</span><span class="p">,</span> <span class="s1">&apos;1970-01-01 00:00:03.140000&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-163">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-164">大纪元倍数将舍入到最接近的纳秒。</span></p>
</div>
</div>
</div>
<div class="section" id="generating-ranges-of-timestamps">
<span id="timeseries-daterange"></span><h2><span class="yiyi-st" id="yiyi-165">Generating Ranges of Timestamps</span></h2>
<p><span class="yiyi-st" id="yiyi-166">要生成带有时间戳的索引，可以使用DatetimeIndex或Index构造函数，并传入datetime对象列表：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [36]: </span><span class="n">dates</span> <span class="o">=</span> <span class="p">[</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">)]</span>

<span class="c"># Note the frequency information</span>
<span class="gp">In [37]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DatetimeIndex</span><span class="p">(</span><span class="n">dates</span><span class="p">)</span>

<span class="gp">In [38]: </span><span class="n">index</span>
<span class="gr">Out[38]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2012-05-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-05-02&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-05-03&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>

<span class="c"># Automatically converted to DatetimeIndex</span>
<span class="gp">In [39]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">(</span><span class="n">dates</span><span class="p">)</span>

<span class="gp">In [40]: </span><span class="n">index</span>
<span class="gr">Out[40]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2012-05-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-05-02&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-05-03&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-167">实际上，这变得非常麻烦，因为我们经常需要具有大量时间戳的非常长的索引。</span><span class="yiyi-st" id="yiyi-168">如果我们需要在正常频率上的时间戳，我们可以使用pandas函数<code class="docutils literal"><span class="pre">date_range</span></code>和<code class="docutils literal"><span class="pre">bdate_range</span></code>来创建时间戳索引。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [41]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;2000-1-1&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [42]: </span><span class="n">index</span>
<span class="gr">Out[42]: </span>
<span class="go">DatetimeIndex([&apos;2000-01-31&apos;, &apos;2000-02-29&apos;, &apos;2000-03-31&apos;, &apos;2000-04-30&apos;,</span>
<span class="go">               &apos;2000-05-31&apos;, &apos;2000-06-30&apos;, &apos;2000-07-31&apos;, &apos;2000-08-31&apos;,</span>
<span class="go">               &apos;2000-09-30&apos;, &apos;2000-10-31&apos;,</span>
<span class="go">               ...</span>
<span class="go">               &apos;2082-07-31&apos;, &apos;2082-08-31&apos;, &apos;2082-09-30&apos;, &apos;2082-10-31&apos;,</span>
<span class="go">               &apos;2082-11-30&apos;, &apos;2082-12-31&apos;, &apos;2083-01-31&apos;, &apos;2083-02-28&apos;,</span>
<span class="go">               &apos;2083-03-31&apos;, &apos;2083-04-30&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, length=1000, freq=&apos;M&apos;)</span>

<span class="gp">In [43]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">bdate_range</span><span class="p">(</span><span class="s1">&apos;2012-1-1&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">250</span><span class="p">)</span>

<span class="gp">In [44]: </span><span class="n">index</span>
<span class="gr">Out[44]: </span>
<span class="go">DatetimeIndex([&apos;2012-01-02&apos;, &apos;2012-01-03&apos;, &apos;2012-01-04&apos;, &apos;2012-01-05&apos;,</span>
<span class="go">               &apos;2012-01-06&apos;, &apos;2012-01-09&apos;, &apos;2012-01-10&apos;, &apos;2012-01-11&apos;,</span>
<span class="go">               &apos;2012-01-12&apos;, &apos;2012-01-13&apos;,</span>
<span class="go">               ...</span>
<span class="go">               &apos;2012-12-03&apos;, &apos;2012-12-04&apos;, &apos;2012-12-05&apos;, &apos;2012-12-06&apos;,</span>
<span class="go">               &apos;2012-12-07&apos;, &apos;2012-12-10&apos;, &apos;2012-12-11&apos;, &apos;2012-12-12&apos;,</span>
<span class="go">               &apos;2012-12-13&apos;, &apos;2012-12-14&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, length=250, freq=&apos;B&apos;)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-169">类似<code class="docutils literal"><span class="pre">date_range</span></code>和<code class="docutils literal"><span class="pre">bdate_range</span></code>的便利功能使用各种频率别名。</span><span class="yiyi-st" id="yiyi-170"><code class="docutils literal"><span class="pre">date_range</span></code>的默认频率为<strong>日历日</strong>，而<code class="docutils literal"><span class="pre">bdate_range</span></code>的默认频率为<strong>工作日</strong></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [45]: </span><span class="n">start</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2011</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

<span class="gp">In [46]: </span><span class="n">end</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

<span class="gp">In [47]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">)</span>

<span class="gp">In [48]: </span><span class="n">rng</span>
<span class="gr">Out[48]: </span>
<span class="go">DatetimeIndex([&apos;2011-01-01&apos;, &apos;2011-01-02&apos;, &apos;2011-01-03&apos;, &apos;2011-01-04&apos;,</span>
<span class="go">               &apos;2011-01-05&apos;, &apos;2011-01-06&apos;, &apos;2011-01-07&apos;, &apos;2011-01-08&apos;,</span>
<span class="go">               &apos;2011-01-09&apos;, &apos;2011-01-10&apos;,</span>
<span class="go">               ...</span>
<span class="go">               &apos;2011-12-23&apos;, &apos;2011-12-24&apos;, &apos;2011-12-25&apos;, &apos;2011-12-26&apos;,</span>
<span class="go">               &apos;2011-12-27&apos;, &apos;2011-12-28&apos;, &apos;2011-12-29&apos;, &apos;2011-12-30&apos;,</span>
<span class="go">               &apos;2011-12-31&apos;, &apos;2012-01-01&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, length=366, freq=&apos;D&apos;)</span>

<span class="gp">In [49]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">bdate_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">)</span>

<span class="gp">In [50]: </span><span class="n">rng</span>
<span class="gr">Out[50]: </span>
<span class="go">DatetimeIndex([&apos;2011-01-03&apos;, &apos;2011-01-04&apos;, &apos;2011-01-05&apos;, &apos;2011-01-06&apos;,</span>
<span class="go">               &apos;2011-01-07&apos;, &apos;2011-01-10&apos;, &apos;2011-01-11&apos;, &apos;2011-01-12&apos;,</span>
<span class="go">               &apos;2011-01-13&apos;, &apos;2011-01-14&apos;,</span>
<span class="go">               ...</span>
<span class="go">               &apos;2011-12-19&apos;, &apos;2011-12-20&apos;, &apos;2011-12-21&apos;, &apos;2011-12-22&apos;,</span>
<span class="go">               &apos;2011-12-23&apos;, &apos;2011-12-26&apos;, &apos;2011-12-27&apos;, &apos;2011-12-28&apos;,</span>
<span class="go">               &apos;2011-12-29&apos;, &apos;2011-12-30&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, length=260, freq=&apos;B&apos;)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-171"><code class="docutils literal"><span class="pre">date_range</span></code>和<code class="docutils literal"><span class="pre">bdate_range</span></code>可以使用<code class="docutils literal"><span class="pre">start</span></code>，<code class="docutils literal"><span class="pre">end</span></code>等参数的各种组合轻松生成日期范围， <code class="docutils literal"><span class="pre">periods</span></code>和<code class="docutils literal"><span class="pre">freq</span></code>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [51]: </span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;BM&apos;</span><span class="p">)</span>
<span class="gr">Out[51]: </span>
<span class="go">DatetimeIndex([&apos;2011-01-31&apos;, &apos;2011-02-28&apos;, &apos;2011-03-31&apos;, &apos;2011-04-29&apos;,</span>
<span class="go">               &apos;2011-05-31&apos;, &apos;2011-06-30&apos;, &apos;2011-07-29&apos;, &apos;2011-08-31&apos;,</span>
<span class="go">               &apos;2011-09-30&apos;, &apos;2011-10-31&apos;, &apos;2011-11-30&apos;, &apos;2011-12-30&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;BM&apos;)</span>

<span class="gp">In [52]: </span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;W&apos;</span><span class="p">)</span>
<span class="gr">Out[52]: </span>
<span class="go">DatetimeIndex([&apos;2011-01-02&apos;, &apos;2011-01-09&apos;, &apos;2011-01-16&apos;, &apos;2011-01-23&apos;,</span>
<span class="go">               &apos;2011-01-30&apos;, &apos;2011-02-06&apos;, &apos;2011-02-13&apos;, &apos;2011-02-20&apos;,</span>
<span class="go">               &apos;2011-02-27&apos;, &apos;2011-03-06&apos;, &apos;2011-03-13&apos;, &apos;2011-03-20&apos;,</span>
<span class="go">               &apos;2011-03-27&apos;, &apos;2011-04-03&apos;, &apos;2011-04-10&apos;, &apos;2011-04-17&apos;,</span>
<span class="go">               &apos;2011-04-24&apos;, &apos;2011-05-01&apos;, &apos;2011-05-08&apos;, &apos;2011-05-15&apos;,</span>
<span class="go">               &apos;2011-05-22&apos;, &apos;2011-05-29&apos;, &apos;2011-06-05&apos;, &apos;2011-06-12&apos;,</span>
<span class="go">               &apos;2011-06-19&apos;, &apos;2011-06-26&apos;, &apos;2011-07-03&apos;, &apos;2011-07-10&apos;,</span>
<span class="go">               &apos;2011-07-17&apos;, &apos;2011-07-24&apos;, &apos;2011-07-31&apos;, &apos;2011-08-07&apos;,</span>
<span class="go">               &apos;2011-08-14&apos;, &apos;2011-08-21&apos;, &apos;2011-08-28&apos;, &apos;2011-09-04&apos;,</span>
<span class="go">               &apos;2011-09-11&apos;, &apos;2011-09-18&apos;, &apos;2011-09-25&apos;, &apos;2011-10-02&apos;,</span>
<span class="go">               &apos;2011-10-09&apos;, &apos;2011-10-16&apos;, &apos;2011-10-23&apos;, &apos;2011-10-30&apos;,</span>
<span class="go">               &apos;2011-11-06&apos;, &apos;2011-11-13&apos;, &apos;2011-11-20&apos;, &apos;2011-11-27&apos;,</span>
<span class="go">               &apos;2011-12-04&apos;, &apos;2011-12-11&apos;, &apos;2011-12-18&apos;, &apos;2011-12-25&apos;,</span>
<span class="go">               &apos;2012-01-01&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;W-SUN&apos;)</span>

<span class="gp">In [53]: </span><span class="n">pd</span><span class="o">.</span><span class="n">bdate_range</span><span class="p">(</span><span class="n">end</span><span class="o">=</span><span class="n">end</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
<span class="gr">Out[53]: </span>
<span class="go">DatetimeIndex([&apos;2011-12-05&apos;, &apos;2011-12-06&apos;, &apos;2011-12-07&apos;, &apos;2011-12-08&apos;,</span>
<span class="go">               &apos;2011-12-09&apos;, &apos;2011-12-12&apos;, &apos;2011-12-13&apos;, &apos;2011-12-14&apos;,</span>
<span class="go">               &apos;2011-12-15&apos;, &apos;2011-12-16&apos;, &apos;2011-12-19&apos;, &apos;2011-12-20&apos;,</span>
<span class="go">               &apos;2011-12-21&apos;, &apos;2011-12-22&apos;, &apos;2011-12-23&apos;, &apos;2011-12-26&apos;,</span>
<span class="go">               &apos;2011-12-27&apos;, &apos;2011-12-28&apos;, &apos;2011-12-29&apos;, &apos;2011-12-30&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;B&apos;)</span>

<span class="gp">In [54]: </span><span class="n">pd</span><span class="o">.</span><span class="n">bdate_range</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="n">start</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
<span class="gr">Out[54]: </span>
<span class="go">DatetimeIndex([&apos;2011-01-03&apos;, &apos;2011-01-04&apos;, &apos;2011-01-05&apos;, &apos;2011-01-06&apos;,</span>
<span class="go">               &apos;2011-01-07&apos;, &apos;2011-01-10&apos;, &apos;2011-01-11&apos;, &apos;2011-01-12&apos;,</span>
<span class="go">               &apos;2011-01-13&apos;, &apos;2011-01-14&apos;, &apos;2011-01-17&apos;, &apos;2011-01-18&apos;,</span>
<span class="go">               &apos;2011-01-19&apos;, &apos;2011-01-20&apos;, &apos;2011-01-21&apos;, &apos;2011-01-24&apos;,</span>
<span class="go">               &apos;2011-01-25&apos;, &apos;2011-01-26&apos;, &apos;2011-01-27&apos;, &apos;2011-01-28&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;B&apos;)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-172">开始和结束日期严格包含。</span><span class="yiyi-st" id="yiyi-173">因此，如果指定，则不会生成这些日期之外的任何日期。</span></p>
</div>
<div class="section" id="timestamp-limitations">
<span id="timeseries-timestamp-limits"></span><h2><span class="yiyi-st" id="yiyi-174">Timestamp limitations</span></h2>
<p><span class="yiyi-st" id="yiyi-175">由于pandas表示纳秒分辨率的时间戳，可以使用64位整数表示的时间间隔限制为大约584年：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [55]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="o">.</span><span class="n">min</span>
<span class="gr">Out[55]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;1677-09-21 00:12:43.145225&apos;</span><span class="p">)</span>

<span class="gp">In [56]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="o">.</span><span class="n">max</span>
<span class="gr">Out[56]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2262-04-11 23:47:16.854775807&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-176">有关如何在这些边界之外表示数据的方法，请参见<a class="reference internal" href="#timeseries-oob"><span class="std std-ref">here</span></a>。</span></p>
</div>
<div class="section" id="datetimeindex">
<span id="timeseries-datetimeindex"></span><h2><span class="yiyi-st" id="yiyi-177">DatetimeIndex</span></h2>
<p><span class="yiyi-st" id="yiyi-178"><code class="docutils literal"><span class="pre">DatetimeIndex</span></code>的主要用途之一是作为pandas对象的索引。</span><span class="yiyi-st" id="yiyi-179"><code class="docutils literal"><span class="pre">DatetimeIndex</span></code>类包含许多与时间相关的优化：</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-180">对于各种偏移量的大范围的日期被预先计算并缓存在引擎盖下，以便使得生成随后的日期范围非常快（只需要抓住切片）</span></li>
<li><span class="yiyi-st" id="yiyi-181">使用pandas对象上的<code class="docutils literal"><span class="pre">shift</span></code>和<code class="docutils literal"><span class="pre">tshift</span></code>方法快速移位</span></li>
<li><span class="yiyi-st" id="yiyi-182">具有相同频率的重叠DatetimeIndex对象的联合非常快（对于快速数据对齐很重要）</span></li>
<li><span class="yiyi-st" id="yiyi-183">通过<code class="docutils literal"><span class="pre">year</span></code>，<code class="docutils literal"><span class="pre">month</span></code>等属性快速访问日期字段</span></li>
<li><span class="yiyi-st" id="yiyi-184">正则化函数如<code class="docutils literal"><span class="pre">snap</span></code>和非常快的<code class="docutils literal"><span class="pre">asof</span></code>逻辑</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-185">DatetimeIndex对象具有常规索引对象的所有基本功能和一个高级时间序列特定方法的smorgasbord，以便于频率处理。</span></p>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-186">也可以看看</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-187"><a class="reference internal" href="basics.html#basics-reindexing"><span class="std std-ref">Reindexing methods</span></a></span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-188">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-189">虽然pandas不强迫你有一个排序的日期索引，但如果日期未排序，这些方法中的一些可能具有意外或不正确的行为。</span><span class="yiyi-st" id="yiyi-190">所以请小心。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-191"><code class="docutils literal"><span class="pre">DatetimeIndex</span></code>可以像常规索引一样使用，并提供其所有智能功能，如选择，切片等。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [57]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;BM&apos;</span><span class="p">)</span>

<span class="gp">In [58]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">rng</span><span class="p">)),</span> <span class="n">index</span><span class="o">=</span><span class="n">rng</span><span class="p">)</span>

<span class="gp">In [59]: </span><span class="n">ts</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[59]: </span>
<span class="go">DatetimeIndex([&apos;2011-01-31&apos;, &apos;2011-02-28&apos;, &apos;2011-03-31&apos;, &apos;2011-04-29&apos;,</span>
<span class="go">               &apos;2011-05-31&apos;, &apos;2011-06-30&apos;, &apos;2011-07-29&apos;, &apos;2011-08-31&apos;,</span>
<span class="go">               &apos;2011-09-30&apos;, &apos;2011-10-31&apos;, &apos;2011-11-30&apos;, &apos;2011-12-30&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;BM&apos;)</span>

<span class="gp">In [60]: </span><span class="n">ts</span><span class="p">[:</span><span class="mi">5</span><span class="p">]</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[60]: </span>
<span class="go">DatetimeIndex([&apos;2011-01-31&apos;, &apos;2011-02-28&apos;, &apos;2011-03-31&apos;, &apos;2011-04-29&apos;,</span>
<span class="go">               &apos;2011-05-31&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;BM&apos;)</span>

<span class="gp">In [61]: </span><span class="n">ts</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[61]: </span>
<span class="go">DatetimeIndex([&apos;2011-01-31&apos;, &apos;2011-03-31&apos;, &apos;2011-05-31&apos;, &apos;2011-07-29&apos;,</span>
<span class="go">               &apos;2011-09-30&apos;, &apos;2011-11-30&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;2BM&apos;)</span>
</pre></div>
</div>
<div class="section" id="datetimeindex-partial-string-indexing">
<span id="timeseries-partialindexing"></span><h3><span class="yiyi-st" id="yiyi-192">DatetimeIndex Partial String Indexing</span></h3>
<p><span class="yiyi-st" id="yiyi-193">您可以传递解析为日期的日期和字符串作为索引参数：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [62]: </span><span class="n">ts</span><span class="p">[</span><span class="s1">&apos;1/31/2011&apos;</span><span class="p">]</span>
<span class="gr">Out[62]: </span><span class="o">-</span><span class="mf">1.2812473076599531</span>

<span class="gp">In [63]: </span><span class="n">ts</span><span class="p">[</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2011</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">25</span><span class="p">):]</span>
<span class="gr">Out[63]: </span>
<span class="go">2011-12-30    0.687738</span>
<span class="go">Freq: BM, dtype: float64</span>

<span class="gp">In [64]: </span><span class="n">ts</span><span class="p">[</span><span class="s1">&apos;10/31/2011&apos;</span><span class="p">:</span><span class="s1">&apos;12/31/2011&apos;</span><span class="p">]</span>
<span class="gr">Out[64]: </span>
<span class="go">2011-10-31    0.149748</span>
<span class="go">2011-11-30   -0.732339</span>
<span class="go">2011-12-30    0.687738</span>
<span class="go">Freq: BM, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-194">为了方便访问较长的时间序列，您还可以将年或年和月作为字符串传入：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [65]: </span><span class="n">ts</span><span class="p">[</span><span class="s1">&apos;2011&apos;</span><span class="p">]</span>
<span class="gr">Out[65]: </span>
<span class="go">2011-01-31   -1.281247</span>
<span class="go">2011-02-28   -0.727707</span>
<span class="go">2011-03-31   -0.121306</span>
<span class="go">2011-04-29   -0.097883</span>
<span class="go">2011-05-31    0.695775</span>
<span class="go">2011-06-30    0.341734</span>
<span class="go">2011-07-29    0.959726</span>
<span class="go">2011-08-31   -1.110336</span>
<span class="go">2011-09-30   -0.619976</span>
<span class="go">2011-10-31    0.149748</span>
<span class="go">2011-11-30   -0.732339</span>
<span class="go">2011-12-30    0.687738</span>
<span class="go">Freq: BM, dtype: float64</span>

<span class="gp">In [66]: </span><span class="n">ts</span><span class="p">[</span><span class="s1">&apos;2011-6&apos;</span><span class="p">]</span>
<span class="gr">Out[66]: </span>
<span class="go">2011-06-30    0.341734</span>
<span class="go">Freq: BM, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-195">这种类型的切片将在具有<code class="docutils literal"><span class="pre">DateTimeIndex</span></code>的DataFrame上工作。</span><span class="yiyi-st" id="yiyi-196">由于部分字符串选择是标签分割的一种形式，所以包括端点<strong>。</strong></span><span class="yiyi-st" id="yiyi-197">这将包括所包含日期的匹配时间。</span><span class="yiyi-st" id="yiyi-198">这里有一个例子：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [67]: </span><span class="n">dft</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">100000</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span>
<span class="gp">   ....:</span>                    <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">],</span>
<span class="gp">   ....:</span>                    <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">,</span><span class="n">periods</span><span class="o">=</span><span class="mi">100000</span><span class="p">,</span><span class="n">freq</span><span class="o">=</span><span class="s1">&apos;T&apos;</span><span class="p">))</span>
<span class="gp">   ....:</span> 

<span class="gp">In [68]: </span><span class="n">dft</span>
<span class="gr">Out[68]: </span>
<span class="go">                            A</span>
<span class="go">2013-01-01 00:00:00  0.176444</span>
<span class="go">2013-01-01 00:01:00  0.403310</span>
<span class="go">2013-01-01 00:02:00 -0.154951</span>
<span class="go">2013-01-01 00:03:00  0.301624</span>
<span class="go">2013-01-01 00:04:00 -2.179861</span>
<span class="go">2013-01-01 00:05:00 -1.369849</span>
<span class="go">2013-01-01 00:06:00 -0.954208</span>
<span class="go">...                       ...</span>
<span class="go">2013-03-11 10:33:00 -0.293083</span>
<span class="go">2013-03-11 10:34:00 -0.059881</span>
<span class="go">2013-03-11 10:35:00  1.252450</span>
<span class="go">2013-03-11 10:36:00  0.046611</span>
<span class="go">2013-03-11 10:37:00  0.059478</span>
<span class="go">2013-03-11 10:38:00 -0.286539</span>
<span class="go">2013-03-11 10:39:00  0.841669</span>

<span class="go">[100000 rows x 1 columns]</span>

<span class="gp">In [69]: </span><span class="n">dft</span><span class="p">[</span><span class="s1">&apos;2013&apos;</span><span class="p">]</span>
<span class="gr">Out[69]: </span>
<span class="go">                            A</span>
<span class="go">2013-01-01 00:00:00  0.176444</span>
<span class="go">2013-01-01 00:01:00  0.403310</span>
<span class="go">2013-01-01 00:02:00 -0.154951</span>
<span class="go">2013-01-01 00:03:00  0.301624</span>
<span class="go">2013-01-01 00:04:00 -2.179861</span>
<span class="go">2013-01-01 00:05:00 -1.369849</span>
<span class="go">2013-01-01 00:06:00 -0.954208</span>
<span class="go">...                       ...</span>
<span class="go">2013-03-11 10:33:00 -0.293083</span>
<span class="go">2013-03-11 10:34:00 -0.059881</span>
<span class="go">2013-03-11 10:35:00  1.252450</span>
<span class="go">2013-03-11 10:36:00  0.046611</span>
<span class="go">2013-03-11 10:37:00  0.059478</span>
<span class="go">2013-03-11 10:38:00 -0.286539</span>
<span class="go">2013-03-11 10:39:00  0.841669</span>

<span class="go">[100000 rows x 1 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-199">这会从当月的第一个时间开始，包括该月的最后一个日期和时间</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [70]: </span><span class="n">dft</span><span class="p">[</span><span class="s1">&apos;2013-1&apos;</span><span class="p">:</span><span class="s1">&apos;2013-2&apos;</span><span class="p">]</span>
<span class="gr">Out[70]: </span>
<span class="go">                            A</span>
<span class="go">2013-01-01 00:00:00  0.176444</span>
<span class="go">2013-01-01 00:01:00  0.403310</span>
<span class="go">2013-01-01 00:02:00 -0.154951</span>
<span class="go">2013-01-01 00:03:00  0.301624</span>
<span class="go">2013-01-01 00:04:00 -2.179861</span>
<span class="go">2013-01-01 00:05:00 -1.369849</span>
<span class="go">2013-01-01 00:06:00 -0.954208</span>
<span class="go">...                       ...</span>
<span class="go">2013-02-28 23:53:00  0.103114</span>
<span class="go">2013-02-28 23:54:00 -1.303422</span>
<span class="go">2013-02-28 23:55:00  0.451943</span>
<span class="go">2013-02-28 23:56:00  0.220534</span>
<span class="go">2013-02-28 23:57:00 -1.624220</span>
<span class="go">2013-02-28 23:58:00  0.093915</span>
<span class="go">2013-02-28 23:59:00 -1.087454</span>

<span class="go">[84960 rows x 1 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-200">这指定了包括最后一天的所有时间的停止时间<strong></strong></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [71]: </span><span class="n">dft</span><span class="p">[</span><span class="s1">&apos;2013-1&apos;</span><span class="p">:</span><span class="s1">&apos;2013-2-28&apos;</span><span class="p">]</span>
<span class="gr">Out[71]: </span>
<span class="go">                            A</span>
<span class="go">2013-01-01 00:00:00  0.176444</span>
<span class="go">2013-01-01 00:01:00  0.403310</span>
<span class="go">2013-01-01 00:02:00 -0.154951</span>
<span class="go">2013-01-01 00:03:00  0.301624</span>
<span class="go">2013-01-01 00:04:00 -2.179861</span>
<span class="go">2013-01-01 00:05:00 -1.369849</span>
<span class="go">2013-01-01 00:06:00 -0.954208</span>
<span class="go">...                       ...</span>
<span class="go">2013-02-28 23:53:00  0.103114</span>
<span class="go">2013-02-28 23:54:00 -1.303422</span>
<span class="go">2013-02-28 23:55:00  0.451943</span>
<span class="go">2013-02-28 23:56:00  0.220534</span>
<span class="go">2013-02-28 23:57:00 -1.624220</span>
<span class="go">2013-02-28 23:58:00  0.093915</span>
<span class="go">2013-02-28 23:59:00 -1.087454</span>

<span class="go">[84960 rows x 1 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-201">这指定了<strong>精确</strong>停止时间（与上述不同）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [72]: </span><span class="n">dft</span><span class="p">[</span><span class="s1">&apos;2013-1&apos;</span><span class="p">:</span><span class="s1">&apos;2013-2-28 00:00:00&apos;</span><span class="p">]</span>
<span class="gr">Out[72]: </span>
<span class="go">                            A</span>
<span class="go">2013-01-01 00:00:00  0.176444</span>
<span class="go">2013-01-01 00:01:00  0.403310</span>
<span class="go">2013-01-01 00:02:00 -0.154951</span>
<span class="go">2013-01-01 00:03:00  0.301624</span>
<span class="go">2013-01-01 00:04:00 -2.179861</span>
<span class="go">2013-01-01 00:05:00 -1.369849</span>
<span class="go">2013-01-01 00:06:00 -0.954208</span>
<span class="go">...                       ...</span>
<span class="go">2013-02-27 23:54:00  0.897051</span>
<span class="go">2013-02-27 23:55:00 -0.309230</span>
<span class="go">2013-02-27 23:56:00  1.944713</span>
<span class="go">2013-02-27 23:57:00  0.369265</span>
<span class="go">2013-02-27 23:58:00  0.053071</span>
<span class="go">2013-02-27 23:59:00 -0.019734</span>
<span class="go">2013-02-28 00:00:00  1.388189</span>

<span class="go">[83521 rows x 1 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-202">我们停在所包含的端点，因为它是索引的一部分</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [73]: </span><span class="n">dft</span><span class="p">[</span><span class="s1">&apos;2013-1-15&apos;</span><span class="p">:</span><span class="s1">&apos;2013-1-15 12:30:00&apos;</span><span class="p">]</span>
<span class="gr">Out[73]: </span>
<span class="go">                            A</span>
<span class="go">2013-01-15 00:00:00  0.501288</span>
<span class="go">2013-01-15 00:01:00 -0.605198</span>
<span class="go">2013-01-15 00:02:00  0.215146</span>
<span class="go">2013-01-15 00:03:00  0.924732</span>
<span class="go">2013-01-15 00:04:00 -2.228519</span>
<span class="go">2013-01-15 00:05:00  1.517331</span>
<span class="go">2013-01-15 00:06:00 -1.188774</span>
<span class="go">...                       ...</span>
<span class="go">2013-01-15 12:24:00  1.358314</span>
<span class="go">2013-01-15 12:25:00 -0.737727</span>
<span class="go">2013-01-15 12:26:00  1.838323</span>
<span class="go">2013-01-15 12:27:00 -0.774090</span>
<span class="go">2013-01-15 12:28:00  0.622261</span>
<span class="go">2013-01-15 12:29:00 -0.631649</span>
<span class="go">2013-01-15 12:30:00  0.193284</span>

<span class="go">[751 rows x 1 columns]</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-203">警告</span></p>
<p><span class="yiyi-st" id="yiyi-204">以下选择将产生<code class="docutils literal"><span class="pre">KeyError</span></code>；否则这种选择方法将与pandas中的其他选择方法不一致（因为这不是<em>切片</em>，也不解析为一个）</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dft</span><span class="p">[</span><span class="s1">&apos;2013-1-15 12:30:00&apos;</span><span class="p">]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-205">要选择单个行，请使用<code class="docutils literal"><span class="pre">.loc</span></code></span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [74]: </span><span class="n">dft</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&apos;2013-1-15 12:30:00&apos;</span><span class="p">]</span>
<span class="gr">Out[74]: </span>
<span class="go">A    0.193284</span>
<span class="go">Name: 2013-01-15 12:30:00, dtype: float64</span>
</pre></div>
</div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-206"><span class="versionmodified">版本0.18.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-207">DatetimeIndex部分字符串索引也适用于具有<code class="docutils literal"><span class="pre">MultiIndex</span></code>的DataFrames。</span><span class="yiyi-st" id="yiyi-208">例如：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [75]: </span><span class="n">dft2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="gp">   ....:</span>                     <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">],</span>
<span class="gp">   ....:</span>                     <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_product</span><span class="p">([</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">,</span>
<span class="gp">   ....:</span>                                                                     <span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
<span class="gp">   ....:</span>                                                                     <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;12H&apos;</span><span class="p">),</span>
<span class="gp">   ....:</span>                                                      <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">]]))</span>
<span class="gp">   ....:</span> 

<span class="gp">In [76]: </span><span class="n">dft2</span>
<span class="gr">Out[76]: </span>
<span class="go">                              A</span>
<span class="go">2013-01-01 00:00:00 a -0.659574</span>
<span class="go">                    b  1.494522</span>
<span class="go">2013-01-01 12:00:00 a -0.778425</span>
<span class="go">                    b -0.253355</span>
<span class="go">2013-01-02 00:00:00 a -2.816159</span>
<span class="go">                    b -1.210929</span>
<span class="go">2013-01-02 12:00:00 a  0.144669</span>
<span class="go">...                         ...</span>
<span class="go">2013-01-04 00:00:00 b -1.624463</span>
<span class="go">2013-01-04 12:00:00 a  0.056912</span>
<span class="go">                    b  0.149867</span>
<span class="go">2013-01-05 00:00:00 a -1.256173</span>
<span class="go">                    b  2.324544</span>
<span class="go">2013-01-05 12:00:00 a -1.067396</span>
<span class="go">                    b -0.660996</span>

<span class="go">[20 rows x 1 columns]</span>

<span class="gp">In [77]: </span><span class="n">dft2</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&apos;2013-01-05&apos;</span><span class="p">]</span>
<span class="gr">Out[77]: </span>
<span class="go">                              A</span>
<span class="go">2013-01-05 00:00:00 a -1.256173</span>
<span class="go">                    b  2.324544</span>
<span class="go">2013-01-05 12:00:00 a -1.067396</span>
<span class="go">                    b -0.660996</span>

<span class="gp">In [78]: </span><span class="n">idx</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">IndexSlice</span>

<span class="gp">In [79]: </span><span class="n">dft2</span> <span class="o">=</span> <span class="n">dft2</span><span class="o">.</span><span class="n">swaplevel</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">sort_index</span><span class="p">()</span>

<span class="gp">In [80]: </span><span class="n">dft2</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">idx</span><span class="p">[:,</span> <span class="s1">&apos;2013-01-05&apos;</span><span class="p">],</span> <span class="p">:]</span>
<span class="gr">Out[80]: </span>
<span class="go">                              A</span>
<span class="go">a 2013-01-05 00:00:00 -1.256173</span>
<span class="go">  2013-01-05 12:00:00 -1.067396</span>
<span class="go">b 2013-01-05 00:00:00  2.324544</span>
<span class="go">  2013-01-05 12:00:00 -0.660996</span>
</pre></div>
</div>
</div>
<div class="section" id="datetime-indexing">
<h3><span class="yiyi-st" id="yiyi-209">Datetime Indexing</span></h3>
<p><span class="yiyi-st" id="yiyi-210">使用部分字符串索引<code class="docutils literal"><span class="pre">DateTimeIndex</span></code>取决于句点的“准确性”，换句话说，间隔与索引的频率之间的具体程度。</span><span class="yiyi-st" id="yiyi-211">相反，使用datetime对象的索引是精确的，因为对象具有确切的含义。</span><span class="yiyi-st" id="yiyi-212">这些也遵循<em>的语义，包括两个端点</em>。</span></p>
<p><span class="yiyi-st" id="yiyi-213">这些<code class="docutils literal"><span class="pre">datetime</span></code>对象是特定的<code class="docutils literal"><span class="pre">小时，</span> <span class="pre">分钟，</span></code>和<code class="docutils literal"><span class="pre">seconds</span></code>没有明确指定（它们是<code class="docutils literal"><span class="pre">0</span></code>）。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [81]: </span><span class="n">dft</span><span class="p">[</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2013</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">):</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2013</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">28</span><span class="p">)]</span>
<span class="gr">Out[81]: </span>
<span class="go">                            A</span>
<span class="go">2013-01-01 00:00:00  0.176444</span>
<span class="go">2013-01-01 00:01:00  0.403310</span>
<span class="go">2013-01-01 00:02:00 -0.154951</span>
<span class="go">2013-01-01 00:03:00  0.301624</span>
<span class="go">2013-01-01 00:04:00 -2.179861</span>
<span class="go">2013-01-01 00:05:00 -1.369849</span>
<span class="go">2013-01-01 00:06:00 -0.954208</span>
<span class="go">...                       ...</span>
<span class="go">2013-02-27 23:54:00  0.897051</span>
<span class="go">2013-02-27 23:55:00 -0.309230</span>
<span class="go">2013-02-27 23:56:00  1.944713</span>
<span class="go">2013-02-27 23:57:00  0.369265</span>
<span class="go">2013-02-27 23:58:00  0.053071</span>
<span class="go">2013-02-27 23:59:00 -0.019734</span>
<span class="go">2013-02-28 00:00:00  1.388189</span>

<span class="go">[83521 rows x 1 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-214">没有默认值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [82]: </span><span class="n">dft</span><span class="p">[</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2013</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2013</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">28</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">0</span><span class="p">)]</span>
<span class="gr">Out[82]: </span>
<span class="go">                            A</span>
<span class="go">2013-01-01 10:12:00 -0.246733</span>
<span class="go">2013-01-01 10:13:00 -1.429225</span>
<span class="go">2013-01-01 10:14:00 -1.265339</span>
<span class="go">2013-01-01 10:15:00  0.710986</span>
<span class="go">2013-01-01 10:16:00 -0.818200</span>
<span class="go">2013-01-01 10:17:00  0.543542</span>
<span class="go">2013-01-01 10:18:00  1.577713</span>
<span class="go">...                       ...</span>
<span class="go">2013-02-28 10:06:00  0.311249</span>
<span class="go">2013-02-28 10:07:00  2.366080</span>
<span class="go">2013-02-28 10:08:00 -0.490372</span>
<span class="go">2013-02-28 10:09:00  0.373340</span>
<span class="go">2013-02-28 10:10:00  0.638442</span>
<span class="go">2013-02-28 10:11:00  1.330135</span>
<span class="go">2013-02-28 10:12:00 -0.945450</span>

<span class="go">[83521 rows x 1 columns]</span>
</pre></div>
</div>
</div>
<div class="section" id="truncating-fancy-indexing">
<h3><span class="yiyi-st" id="yiyi-215">Truncating &amp; Fancy Indexing</span></h3>
<p><span class="yiyi-st" id="yiyi-216">提供了等效于切片的<code class="docutils literal"><span class="pre">truncate</span></code>便利函数：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [83]: </span><span class="n">ts</span><span class="o">.</span><span class="n">truncate</span><span class="p">(</span><span class="n">before</span><span class="o">=</span><span class="s1">&apos;10/31/2011&apos;</span><span class="p">,</span> <span class="n">after</span><span class="o">=</span><span class="s1">&apos;12/31/2011&apos;</span><span class="p">)</span>
<span class="gr">Out[83]: </span>
<span class="go">2011-10-31    0.149748</span>
<span class="go">2011-11-30   -0.732339</span>
<span class="go">2011-12-30    0.687738</span>
<span class="go">Freq: BM, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-217">即使是打破DatetimeIndex的频率规律性的复杂花式索引也会导致<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>（但频率丢失）：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [84]: </span><span class="n">ts</span><span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">6</span><span class="p">]]</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[84]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2011-01-31&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-03-31&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-07-29&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="time-date-components">
<span id="timeseries-offsets"></span><h3><span class="yiyi-st" id="yiyi-218">Time/Date Components</span></h3>
<p><span class="yiyi-st" id="yiyi-219">有几个时间/日期属性可以从<code class="docutils literal"><span class="pre">Timestamp</span></code>或像<code class="docutils literal"><span class="pre">DateTimeIndex</span></code>的时间戳集合访问。</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="19%">
<col width="81%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-220">属性</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-221">描述</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-222">year</span></td>
<td><span class="yiyi-st" id="yiyi-223">datetime的年份</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-224">month</span></td>
<td><span class="yiyi-st" id="yiyi-225">datetime的月份</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-226">day</span></td>
<td><span class="yiyi-st" id="yiyi-227">datetime的日期</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-228">hour</span></td>
<td><span class="yiyi-st" id="yiyi-229">datetime的小时</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-230">minute</span></td>
<td><span class="yiyi-st" id="yiyi-231">datetime的分钟</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-232">second</span></td>
<td><span class="yiyi-st" id="yiyi-233">datetime的秒数</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-234">microsecond</span></td>
<td><span class="yiyi-st" id="yiyi-235">datetime的微秒</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-236">nanosecond</span></td>
<td><span class="yiyi-st" id="yiyi-237">datetime的纳秒</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-238">date</span></td>
<td><span class="yiyi-st" id="yiyi-239">返回datetime.date（不包含时区信息）</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-240">time</span></td>
<td><span class="yiyi-st" id="yiyi-241">返回datetime.time（不包含时区信息）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-242">dayofyear</span></td>
<td><span class="yiyi-st" id="yiyi-243">一年的序数</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-244">weekofyear</span></td>
<td><span class="yiyi-st" id="yiyi-245">一年的周数</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-246">week</span></td>
<td><span class="yiyi-st" id="yiyi-247">一年的周数</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-248">dayofweek</span></td>
<td><span class="yiyi-st" id="yiyi-249">星期几的数字，星期一= 0，星期日= 6</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-250">weekday</span></td>
<td><span class="yiyi-st" id="yiyi-251">星期几的数字，星期一= 0，星期日= 6</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-252">weekday_name</span></td>
<td><span class="yiyi-st" id="yiyi-253">一周中的日期名称（例如：星期五）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-254">quarter</span></td>
<td><span class="yiyi-st" id="yiyi-255">季度：Jan = Mar = 1，Apr-Jun = 2等。</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-256">days_in_month</span></td>
<td><span class="yiyi-st" id="yiyi-257">日期时间的月份中的天数</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-258">is_month_start</span></td>
<td><span class="yiyi-st" id="yiyi-259">逻辑指示是否每月的第一天（由频率定义）</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-260">is_month_end</span></td>
<td><span class="yiyi-st" id="yiyi-261">逻辑指示是否每月的最后一天（由频率定义）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-262">is_quarter_start</span></td>
<td><span class="yiyi-st" id="yiyi-263">逻辑指示季度的第一天（由频率定义）</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-264">is_quarter_end</span></td>
<td><span class="yiyi-st" id="yiyi-265">逻辑指示季度的最后一天（由频率定义）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-266">is_year_start</span></td>
<td><span class="yiyi-st" id="yiyi-267">逻辑指示一年中的第一天（由频率定义）</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-268">is_year_end</span></td>
<td><span class="yiyi-st" id="yiyi-269">逻辑指示一年中的最后一天（由频率定义）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-270">is_leap_year</span></td>
<td><span class="yiyi-st" id="yiyi-271">逻辑指示日期是否属于闰年</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-272">此外，如果您有一个具有datetimelike值的<code class="docutils literal"><span class="pre">Series</span></code>，那么您可以通过<code class="docutils literal"><span class="pre">.dt</span></code>访问器访问这些属性，请参阅<a class="reference internal" href="basics.html#basics-dt-accessors"><span class="std std-ref">docs</span></a></span></p>
</div>
</div>
<div class="section" id="dateoffset-objects">
<h2><span class="yiyi-st" id="yiyi-273">DateOffset objects</span></h2>
<p><span class="yiyi-st" id="yiyi-274">在前面的示例中，我们通过将<a class="reference internal" href="#timeseries-offset-aliases"><span class="std std-ref">frequency strings</span></a>（如“M”，“W”和“BM”）传递到<code class="docutils literal"><span class="pre">freq</span></code>关键字，以各种频率创建DatetimeIndex对象。</span><span class="yiyi-st" id="yiyi-275">在这种情况下，这些频率字符串正被转换为pandas <code class="docutils literal"><span class="pre">DateOffset</span></code>的实例，其表示规则的频率增量。</span><span class="yiyi-st" id="yiyi-276">诸如“月”，“工作日”或“一小时”的特定偏移逻辑表示在其各种子类中。</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="19%">
<col width="81%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-277">班级名称</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-278">描述</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-279">DateOffset</span></td>
<td><span class="yiyi-st" id="yiyi-280">通用偏移类，默认为1个日历日</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-281">BDay</span></td>
<td><span class="yiyi-st" id="yiyi-282">工作日（工作日）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-283">CDay</span></td>
<td><span class="yiyi-st" id="yiyi-284">自定义工作日（实验）</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-285">Week</span></td>
<td><span class="yiyi-st" id="yiyi-286">一周，任选地锚定在一周的一天</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-287">WeekOfMonth</span></td>
<td><span class="yiyi-st" id="yiyi-288">每个月第y周的第x天</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-289">LastWeekOfMonth</span></td>
<td><span class="yiyi-st" id="yiyi-290">每个月最后一周的第x天</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-291">MonthEnd</span></td>
<td><span class="yiyi-st" id="yiyi-292">日历月结束</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-293">MonthBegin</span></td>
<td><span class="yiyi-st" id="yiyi-294">日历月开始</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-295">BMonthEnd</span></td>
<td><span class="yiyi-st" id="yiyi-296">业务月末</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-297">BMonthBegin</span></td>
<td><span class="yiyi-st" id="yiyi-298">商业月开始</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-299">CBMonthEnd</span></td>
<td><span class="yiyi-st" id="yiyi-300">定制业务月末</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-301">CBMonthBegin</span></td>
<td><span class="yiyi-st" id="yiyi-302">定制商业月开始</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-303">SemiMonthEnd</span></td>
<td><span class="yiyi-st" id="yiyi-304">15日（或其他day_of_month）和日历月结束</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-305">SemiMonthBegin</span></td>
<td><span class="yiyi-st" id="yiyi-306">15日（或其他day_of_month）和日历月开始</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-307">QuarterEnd</span></td>
<td><span class="yiyi-st" id="yiyi-308">日历季度末</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-309">QuarterBegin</span></td>
<td><span class="yiyi-st" id="yiyi-310">日历季度开始</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-311">BQuarterEnd</span></td>
<td><span class="yiyi-st" id="yiyi-312">业务季度末</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-313">BQuarterBegin</span></td>
<td><span class="yiyi-st" id="yiyi-314">业务季开始</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-315">FY5253Quarter</span></td>
<td><span class="yiyi-st" id="yiyi-316">零售（又名52-53周）季度</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-317">YearEnd</span></td>
<td><span class="yiyi-st" id="yiyi-318">日历年结束</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-319">YearBegin</span></td>
<td><span class="yiyi-st" id="yiyi-320">日历年开始</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-321">BYearEnd</span></td>
<td><span class="yiyi-st" id="yiyi-322">业务年度结束</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-323">BYearBegin</span></td>
<td><span class="yiyi-st" id="yiyi-324">商业年开始</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-325">FY5253</span></td>
<td><span class="yiyi-st" id="yiyi-326">零售（又名52-53周）一年</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-327">BusinessHour</span></td>
<td><span class="yiyi-st" id="yiyi-328">营业时间</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-329">CustomBusinessHour</span></td>
<td><span class="yiyi-st" id="yiyi-330">定制营业时间</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-331">Hour</span></td>
<td><span class="yiyi-st" id="yiyi-332">一小时</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-333">Minute</span></td>
<td><span class="yiyi-st" id="yiyi-334">等一下</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-335">Second</span></td>
<td><span class="yiyi-st" id="yiyi-336">一秒</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-337">Milli</span></td>
<td><span class="yiyi-st" id="yiyi-338">一毫秒</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-339">Micro</span></td>
<td><span class="yiyi-st" id="yiyi-340">一微秒</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-341">Nano</span></td>
<td><span class="yiyi-st" id="yiyi-342">一纳秒</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-343">基本的<code class="docutils literal"><span class="pre">DateOffset</span></code>采用与<code class="docutils literal"><span class="pre">dateutil.relativedelta</span></code>相同的参数，其工作方式如下：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [85]: </span><span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2008</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

<span class="gp">In [86]: </span><span class="n">d</span> <span class="o">+</span> <span class="n">relativedelta</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">days</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="gr">Out[86]: </span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2008</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">23</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-344">我们可以用<code class="docutils literal"><span class="pre">DateOffset</span></code>做同样的事情：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [87]: </span><span class="kn">from</span> <span class="nn">pandas.tseries.offsets</span> <span class="kn">import</span> <span class="o">*</span>

<span class="gp">In [88]: </span><span class="n">d</span> <span class="o">+</span> <span class="n">DateOffset</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">days</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="gr">Out[88]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2008-12-23 09:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-345"><code class="docutils literal"><span class="pre">DateOffset</span></code>对象的主要功能有：</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-346">可以向/从日期时间对象添加/减去它以获得移位的日期</span></li>
<li><span class="yiyi-st" id="yiyi-347">它可以乘以一个整数（正或负），使得增量将被应用多次</span></li>
<li><span class="yiyi-st" id="yiyi-348">它具有用于将日期向前或向后移动到下一个或上一个“偏移日期”的<code class="docutils literal"><span class="pre">rollforward</span></code>和<code class="docutils literal"><span class="pre">rollback</span></code></span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-349"><code class="docutils literal"><span class="pre">DateOffset</span></code>的子类定义了规定自定义日期增量逻辑的<code class="docutils literal"><span class="pre">apply</span></code>函数，例如添加营业日：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">BDay</span><span class="p">(</span><span class="n">DateOffset</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;DateOffset increments between business days&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="o">...</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [89]: </span><span class="n">d</span> <span class="o">-</span> <span class="mi">5</span> <span class="o">*</span> <span class="n">BDay</span><span class="p">()</span>
<span class="gr">Out[89]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2008-08-11 09:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [90]: </span><span class="n">d</span> <span class="o">+</span> <span class="n">BMonthEnd</span><span class="p">()</span>
<span class="gr">Out[90]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2008-08-29 09:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-350"><code class="docutils literal"><span class="pre">rollforward</span></code>和<code class="docutils literal"><span class="pre">rollback</span></code>方法完全符合您的预期：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [91]: </span><span class="n">d</span>
<span class="gr">Out[91]: </span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2008</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

<span class="gp">In [92]: </span><span class="n">offset</span> <span class="o">=</span> <span class="n">BMonthEnd</span><span class="p">()</span>

<span class="gp">In [93]: </span><span class="n">offset</span><span class="o">.</span><span class="n">rollforward</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="gr">Out[93]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2008-08-29 09:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [94]: </span><span class="n">offset</span><span class="o">.</span><span class="n">rollback</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="gr">Out[94]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2008-07-31 09:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-351">这绝对值得探索<code class="docutils literal"><span class="pre">pandas.tseries.offsets</span></code>模块和类的各种docstrings。</span></p>
<p><span class="yiyi-st" id="yiyi-352">默认情况下，这些操作（<code class="docutils literal"><span class="pre">apply</span></code>，<code class="docutils literal"><span class="pre">rollforward</span></code>和<code class="docutils literal"><span class="pre">rollback</span></code>）保留时间（小时，分钟等）信息。</span><span class="yiyi-st" id="yiyi-353">要重置时间，请在创建偏移实例时使用<code class="docutils literal"><span class="pre">normalize=True</span></code>关键字。</span><span class="yiyi-st" id="yiyi-354">如果<code class="docutils literal"><span class="pre">normalize=True</span></code>，则在应用函数后将结果归一化。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [95]: </span><span class="n">day</span> <span class="o">=</span> <span class="n">Day</span><span class="p">()</span>

<span class="gp">In [96]: </span><span class="n">day</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-01 09:00&apos;</span><span class="p">))</span>
<span class="gr">Out[96]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-02 09:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [97]: </span><span class="n">day</span> <span class="o">=</span> <span class="n">Day</span><span class="p">(</span><span class="n">normalize</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [98]: </span><span class="n">day</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-01 09:00&apos;</span><span class="p">))</span>
<span class="gr">Out[98]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-02 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [99]: </span><span class="n">hour</span> <span class="o">=</span> <span class="n">Hour</span><span class="p">()</span>

<span class="gp">In [100]: </span><span class="n">hour</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-01 22:00&apos;</span><span class="p">))</span>
<span class="gr">Out[100]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-01 23:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [101]: </span><span class="n">hour</span> <span class="o">=</span> <span class="n">Hour</span><span class="p">(</span><span class="n">normalize</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [102]: </span><span class="n">hour</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-01 22:00&apos;</span><span class="p">))</span>
<span class="gr">Out[102]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-01 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [103]: </span><span class="n">hour</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-01 23:00&apos;</span><span class="p">))</span>
<span class="gr">Out[103]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-02 00:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="parametric-offsets">
<h3><span class="yiyi-st" id="yiyi-355">Parametric offsets</span></h3>
<p><span class="yiyi-st" id="yiyi-356">一些偏移可以在被创建时被“参数化”以导致不同的行为。</span><span class="yiyi-st" id="yiyi-357">例如，用于生成周数据的<code class="docutils literal"><span class="pre">Week</span></code>偏移接受<code class="docutils literal"><span class="pre">weekday</span></code>参数，这导致生成的日期总是位于一周的特定日期：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [104]: </span><span class="n">d</span>
<span class="gr">Out[104]: </span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2008</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

<span class="gp">In [105]: </span><span class="n">d</span> <span class="o">+</span> <span class="n">Week</span><span class="p">()</span>
<span class="gr">Out[105]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2008-08-25 09:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [106]: </span><span class="n">d</span> <span class="o">+</span> <span class="n">Week</span><span class="p">(</span><span class="n">weekday</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="gr">Out[106]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2008-08-22 09:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [107]: </span><span class="p">(</span><span class="n">d</span> <span class="o">+</span> <span class="n">Week</span><span class="p">(</span><span class="n">weekday</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span>
<span class="gr">Out[107]: </span><span class="mi">4</span>

<span class="gp">In [108]: </span><span class="n">d</span> <span class="o">-</span> <span class="n">Week</span><span class="p">()</span>
<span class="gr">Out[108]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2008-08-11 09:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-358"><code class="docutils literal"><span class="pre">normalize</span></code>选项将有效用于加法和减法。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [109]: </span><span class="n">d</span> <span class="o">+</span> <span class="n">Week</span><span class="p">(</span><span class="n">normalize</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gr">Out[109]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2008-08-25 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [110]: </span><span class="n">d</span> <span class="o">-</span> <span class="n">Week</span><span class="p">(</span><span class="n">normalize</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gr">Out[110]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2008-08-11 00:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-359">另一个例子是参数化具有特定结束月份的<code class="docutils literal"><span class="pre">YearEnd</span></code>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [111]: </span><span class="n">d</span> <span class="o">+</span> <span class="n">YearEnd</span><span class="p">()</span>
<span class="gr">Out[111]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2008-12-31 09:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [112]: </span><span class="n">d</span> <span class="o">+</span> <span class="n">YearEnd</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>
<span class="gr">Out[112]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2009-06-30 09:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="using-offsets-with-series-datetimeindex">
<span id="timeseries-offsetseries"></span><h3><span class="yiyi-st" id="yiyi-360">Using offsets with <code class="docutils literal"><span class="pre">Series</span></code> / <code class="docutils literal"><span class="pre">DatetimeIndex</span></code></span></h3>
<p><span class="yiyi-st" id="yiyi-361">偏移可以与<code class="docutils literal"><span class="pre">Series</span></code>或<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>一起使用，以将偏移应用于每个元素。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [113]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;2012-01-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-01-03&apos;</span><span class="p">)</span>

<span class="gp">In [114]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">rng</span><span class="p">)</span>

<span class="gp">In [115]: </span><span class="n">rng</span>
<span class="gr">Out[115]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2012-01-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-01-02&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-01-03&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [116]: </span><span class="n">rng</span> <span class="o">+</span> <span class="n">DateOffset</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[116]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2012-03-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-03-02&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-03-03&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [117]: </span><span class="n">s</span> <span class="o">+</span> <span class="n">DateOffset</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[117]: </span>
<span class="go">0   2012-03-01</span>
<span class="go">1   2012-03-02</span>
<span class="go">2   2012-03-03</span>
<span class="go">dtype: datetime64[ns]</span>

<span class="gp">In [118]: </span><span class="n">s</span> <span class="o">-</span> <span class="n">DateOffset</span><span class="p">(</span><span class="n">months</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[118]: </span>
<span class="go">0   2011-11-01</span>
<span class="go">1   2011-11-02</span>
<span class="go">2   2011-11-03</span>
<span class="go">dtype: datetime64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-362">If the offset class maps directly to a <code class="docutils literal"><span class="pre">Timedelta</span></code> (<code class="docutils literal"><span class="pre">Day</span></code>, <code class="docutils literal"><span class="pre">Hour</span></code>, <code class="docutils literal"><span class="pre">Minute</span></code>, <code class="docutils literal"><span class="pre">Second</span></code>, <code class="docutils literal"><span class="pre">Micro</span></code>, <code class="docutils literal"><span class="pre">Milli</span></code>, <code class="docutils literal"><span class="pre">Nano</span></code>) it can be used exactly like a <code class="docutils literal"><span class="pre">Timedelta</span></code> - see the <a class="reference internal" href="timedeltas.html#timedeltas-operations"><span class="std std-ref">Timedelta section</span></a> for more examples.</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [119]: </span><span class="n">s</span> <span class="o">-</span> <span class="n">Day</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[119]: </span>
<span class="go">0   2011-12-30</span>
<span class="go">1   2011-12-31</span>
<span class="go">2   2012-01-01</span>
<span class="go">dtype: datetime64[ns]</span>

<span class="gp">In [120]: </span><span class="n">td</span> <span class="o">=</span> <span class="n">s</span> <span class="o">-</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;2011-12-29&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-12-31&apos;</span><span class="p">))</span>

<span class="gp">In [121]: </span><span class="n">td</span>
<span class="gr">Out[121]: </span>
<span class="go">0   3 days</span>
<span class="go">1   3 days</span>
<span class="go">2   3 days</span>
<span class="go">dtype: timedelta64[ns]</span>

<span class="gp">In [122]: </span><span class="n">td</span> <span class="o">+</span> <span class="n">Minute</span><span class="p">(</span><span class="mi">15</span><span class="p">)</span>
<span class="gr">Out[122]: </span>
<span class="go">0   3 days 00:15:00</span>
<span class="go">1   3 days 00:15:00</span>
<span class="go">2   3 days 00:15:00</span>
<span class="go">dtype: timedelta64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-363">注意，一些偏移（例如<code class="docutils literal"><span class="pre">BQuarterEnd</span></code>）没有向量化实现。</span><span class="yiyi-st" id="yiyi-364">它们仍然可以使用，但可能会显着减慢，并会产生<code class="docutils literal"><span class="pre">PerformanceWarning</span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [123]: </span><span class="n">rng</span> <span class="o">+</span> <span class="n">BQuarterEnd</span><span class="p">()</span>
<span class="gr">Out[123]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2012-03-30&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-03-30&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-03-30&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="custom-business-days-experimental">
<span id="timeseries-custombusinessdays"></span><h3><span class="yiyi-st" id="yiyi-365">Custom Business Days (Experimental)</span></h3>
<p><span class="yiyi-st" id="yiyi-366"><code class="docutils literal"><span class="pre">CDay</span></code>或<code class="docutils literal"><span class="pre">CustomBusinessDay</span></code>类提供了一个参数<code class="docutils literal"><span class="pre">BusinessDay</span></code>类，可用于创建定制的工作日日历，用于说明当地假日和本地周末约定。</span></p>
<p><span class="yiyi-st" id="yiyi-367">作为一个有趣的例子，让我们看看埃及，周五周六周末。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [124]: </span><span class="kn">from</span> <span class="nn">pandas.tseries.offsets</span> <span class="kn">import</span> <span class="n">CustomBusinessDay</span>

<span class="gp">In [125]: </span><span class="n">weekmask_egypt</span> <span class="o">=</span> <span class="s1">&apos;Sun Mon Tue Wed Thu&apos;</span>

<span class="c"># They also observe International Workers&apos; Day so let&apos;s</span>
<span class="c"># add that for a couple of years</span>
<span class="gp">In [126]: </span><span class="n">holidays</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;2012-05-01&apos;</span><span class="p">,</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2013</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">&apos;2014-05-01&apos;</span><span class="p">)]</span>

<span class="gp">In [127]: </span><span class="n">bday_egypt</span> <span class="o">=</span> <span class="n">CustomBusinessDay</span><span class="p">(</span><span class="n">holidays</span><span class="o">=</span><span class="n">holidays</span><span class="p">,</span> <span class="n">weekmask</span><span class="o">=</span><span class="n">weekmask_egypt</span><span class="p">)</span>

<span class="gp">In [128]: </span><span class="n">dt</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2013</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>

<span class="gp">In [129]: </span><span class="n">dt</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">bday_egypt</span>
<span class="gr">Out[129]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2013-05-05 00:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-368">让我们映射到星期几的名字</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [130]: </span><span class="n">dts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="n">bday_egypt</span><span class="p">)</span>

<span class="gp">In [131]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">dts</span><span class="o">.</span><span class="n">weekday</span><span class="p">,</span> <span class="n">dts</span><span class="p">)</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="s1">&apos;Mon Tue Wed Thu Fri Sat Sun&apos;</span><span class="o">.</span><span class="n">split</span><span class="p">()))</span>
<span class="gr">Out[131]: </span>
<span class="go">2013-04-30    Tue</span>
<span class="go">2013-05-02    Thu</span>
<span class="go">2013-05-05    Sun</span>
<span class="go">2013-05-06    Mon</span>
<span class="go">2013-05-07    Tue</span>
<span class="go">Freq: C, dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-369">从v0.14起，假日日历可用于提供假日列表。</span><span class="yiyi-st" id="yiyi-370">有关详细信息，请参阅<a class="reference internal" href="#timeseries-holiday"><span class="std std-ref">holiday calendar</span></a>部分。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [132]: </span><span class="kn">from</span> <span class="nn">pandas.tseries.holiday</span> <span class="kn">import</span> <span class="n">USFederalHolidayCalendar</span>

<span class="gp">In [133]: </span><span class="n">bday_us</span> <span class="o">=</span> <span class="n">CustomBusinessDay</span><span class="p">(</span><span class="n">calendar</span><span class="o">=</span><span class="n">USFederalHolidayCalendar</span><span class="p">())</span>

<span class="c"># Friday before MLK Day</span>
<span class="gp">In [134]: </span><span class="n">dt</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2014</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">17</span><span class="p">)</span>

<span class="c"># Tuesday after MLK Day (Monday is skipped because it&apos;s a holiday)</span>
<span class="gp">In [135]: </span><span class="n">dt</span> <span class="o">+</span> <span class="n">bday_us</span>
<span class="gr">Out[135]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-21 00:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-371">可以以通常的方式定义尊重某个假日日历的每月偏移量。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [136]: </span><span class="kn">from</span> <span class="nn">pandas.tseries.offsets</span> <span class="kn">import</span> <span class="n">CustomBusinessMonthBegin</span>

<span class="gp">In [137]: </span><span class="n">bmth_us</span> <span class="o">=</span> <span class="n">CustomBusinessMonthBegin</span><span class="p">(</span><span class="n">calendar</span><span class="o">=</span><span class="n">USFederalHolidayCalendar</span><span class="p">())</span>

<span class="c"># Skip new years</span>
<span class="gp">In [138]: </span><span class="n">dt</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2013</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">17</span><span class="p">)</span>

<span class="gp">In [139]: </span><span class="n">dt</span> <span class="o">+</span> <span class="n">bmth_us</span>
<span class="gr">Out[139]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-02 00:00:00&apos;</span><span class="p">)</span>

<span class="c"># Define date index with custom offset</span>
<span class="gp">In [140]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DatetimeIndex</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="s1">&apos;20100101&apos;</span><span class="p">,</span><span class="n">end</span><span class="o">=</span><span class="s1">&apos;20120101&apos;</span><span class="p">,</span><span class="n">freq</span><span class="o">=</span><span class="n">bmth_us</span><span class="p">)</span>
<span class="gr">Out[140]: </span>
<span class="go">DatetimeIndex([&apos;2010-01-04&apos;, &apos;2010-02-01&apos;, &apos;2010-03-01&apos;, &apos;2010-04-01&apos;,</span>
<span class="go">               &apos;2010-05-03&apos;, &apos;2010-06-01&apos;, &apos;2010-07-01&apos;, &apos;2010-08-02&apos;,</span>
<span class="go">               &apos;2010-09-01&apos;, &apos;2010-10-01&apos;, &apos;2010-11-01&apos;, &apos;2010-12-01&apos;,</span>
<span class="go">               &apos;2011-01-03&apos;, &apos;2011-02-01&apos;, &apos;2011-03-01&apos;, &apos;2011-04-01&apos;,</span>
<span class="go">               &apos;2011-05-02&apos;, &apos;2011-06-01&apos;, &apos;2011-07-01&apos;, &apos;2011-08-01&apos;,</span>
<span class="go">               &apos;2011-09-01&apos;, &apos;2011-10-03&apos;, &apos;2011-11-01&apos;, &apos;2011-12-01&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;CBMS&apos;)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-372">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-373">频率字符串&apos;C&apos;用于指示使用CustomBusinessDay DateOffset，重要的是要注意，由于CustomBusinessDay是一种参数化类型，CustomBusinessDay的实例可能不同，并且不能从“C”频率字符串中检测到。</span><span class="yiyi-st" id="yiyi-374">因此，用户需要确保“C”频率串在用户的应用中一致地使用。</span></p>
</div>
</div>
<div class="section" id="business-hour">
<span id="timeseries-businesshour"></span><h3><span class="yiyi-st" id="yiyi-375">Business Hour</span></h3>
<p><span class="yiyi-st" id="yiyi-376"><code class="docutils literal"><span class="pre">BusinessHour</span></code>类在<code class="docutils literal"><span class="pre">BusinessDay</span></code>上提供营业时间表示，允许使用特定的开始和结束时间。</span></p>
<p><span class="yiyi-st" id="yiyi-377">默认情况下，<code class="docutils literal"><span class="pre">BusinessHour</span></code>使用上午9:00  -  17:00作为营业时间。</span><span class="yiyi-st" id="yiyi-378">添加<code class="docutils literal"><span class="pre">BusinessHour</span></code>将每小时增加<code class="docutils literal"><span class="pre">Timestamp</span></code>。</span><span class="yiyi-st" id="yiyi-379">如果目标<code class="docutils literal"><span class="pre">Timestamp</span></code>超出营业时间，请移至下一个营业时间，然后将其递增。</span><span class="yiyi-st" id="yiyi-380">如果结果超过营业时间结束，剩余将添加到下一个工作日。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [141]: </span><span class="n">bh</span> <span class="o">=</span> <span class="n">BusinessHour</span><span class="p">()</span>

<span class="gp">In [142]: </span><span class="n">bh</span>
<span class="gr">Out[142]: </span><span class="o">&lt;</span><span class="n">BusinessHour</span><span class="p">:</span> <span class="n">BH</span><span class="o">=</span><span class="mi">09</span><span class="p">:</span><span class="mo">00</span><span class="o">-</span><span class="mi">17</span><span class="p">:</span><span class="mo">00</span><span class="o">&gt;</span>

<span class="c"># 2014-08-01 is Friday</span>
<span class="gp">In [143]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 10:00&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span>
<span class="gr">Out[143]: </span><span class="mi">4</span>

<span class="gp">In [144]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 10:00&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">bh</span>
<span class="gr">Out[144]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 11:00:00&apos;</span><span class="p">)</span>

<span class="c"># Below example is the same as: pd.Timestamp(&apos;2014-08-01 09:00&apos;) + bh</span>
<span class="gp">In [145]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 08:00&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">bh</span>
<span class="gr">Out[145]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 10:00:00&apos;</span><span class="p">)</span>

<span class="c"># If the results is on the end time, move to the next business day</span>
<span class="gp">In [146]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 16:00&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">bh</span>
<span class="gr">Out[146]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-04 09:00:00&apos;</span><span class="p">)</span>

<span class="c"># Remainings are added to the next day</span>
<span class="gp">In [147]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 16:30&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">bh</span>
<span class="gr">Out[147]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-04 09:30:00&apos;</span><span class="p">)</span>

<span class="c"># Adding 2 business hours</span>
<span class="gp">In [148]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 10:00&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">BusinessHour</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[148]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 12:00:00&apos;</span><span class="p">)</span>

<span class="c"># Subtracting 3 business hours</span>
<span class="gp">In [149]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 10:00&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">BusinessHour</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span>
<span class="gr">Out[149]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-07-31 15:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-381">此外，您可以通过关键字指定<code class="docutils literal"><span class="pre">start</span></code>和<code class="docutils literal"><span class="pre">end</span></code>时间。</span><span class="yiyi-st" id="yiyi-382">参数必须为<code class="docutils literal"><span class="pre">str</span></code>，其具有<code class="docutils literal"><span class="pre">hour:minute</span></code>表示形式或<code class="docutils literal"><span class="pre">datetime.time</span></code>实例。</span><span class="yiyi-st" id="yiyi-383">将工作时间指定为秒，微秒和纳秒导致<code class="docutils literal"><span class="pre">ValueError</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [150]: </span><span class="n">bh</span> <span class="o">=</span> <span class="n">BusinessHour</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="s1">&apos;11:00&apos;</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="n">time</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>

<span class="gp">In [151]: </span><span class="n">bh</span>
<span class="gr">Out[151]: </span><span class="o">&lt;</span><span class="n">BusinessHour</span><span class="p">:</span> <span class="n">BH</span><span class="o">=</span><span class="mi">11</span><span class="p">:</span><span class="mo">00</span><span class="o">-</span><span class="mi">20</span><span class="p">:</span><span class="mo">00</span><span class="o">&gt;</span>

<span class="gp">In [152]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 13:00&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">bh</span>
<span class="gr">Out[152]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 14:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [153]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 09:00&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">bh</span>
<span class="gr">Out[153]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 12:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [154]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 18:00&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">bh</span>
<span class="gr">Out[154]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 19:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-384">在<code class="docutils literal"><span class="pre">end</span></code>之后传递<code class="docutils literal"><span class="pre">start</span></code>时间表示午夜营业时间。</span><span class="yiyi-st" id="yiyi-385">在这种情况下，营业时间超过午夜，并与第二天重叠。</span><span class="yiyi-st" id="yiyi-386">有效营业时间的区别在于它是否从有效的<code class="docutils literal"><span class="pre">BusinessDay</span></code>开始。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [155]: </span><span class="n">bh</span> <span class="o">=</span> <span class="n">BusinessHour</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="s1">&apos;17:00&apos;</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">&apos;09:00&apos;</span><span class="p">)</span>

<span class="gp">In [156]: </span><span class="n">bh</span>
<span class="gr">Out[156]: </span><span class="o">&lt;</span><span class="n">BusinessHour</span><span class="p">:</span> <span class="n">BH</span><span class="o">=</span><span class="mi">17</span><span class="p">:</span><span class="mo">00</span><span class="o">-</span><span class="mi">09</span><span class="p">:</span><span class="mo">00</span><span class="o">&gt;</span>

<span class="gp">In [157]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 17:00&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">bh</span>
<span class="gr">Out[157]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 18:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [158]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 23:00&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">bh</span>
<span class="gr">Out[158]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-02 00:00:00&apos;</span><span class="p">)</span>

<span class="c"># Although 2014-08-02 is Satuaday,</span>
<span class="c"># it is valid because it starts from 08-01 (Friday).</span>
<span class="gp">In [159]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-02 04:00&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">bh</span>
<span class="gr">Out[159]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-02 05:00:00&apos;</span><span class="p">)</span>

<span class="c"># Although 2014-08-04 is Monday,</span>
<span class="c"># it is out of business hours because it starts from 08-03 (Sunday).</span>
<span class="gp">In [160]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-04 04:00&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">bh</span>
<span class="gr">Out[160]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-04 18:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-387">将<code class="docutils literal"><span class="pre">BusinessHour.rollforward</span></code>和<code class="docutils literal"><span class="pre">rollback</span></code>应用于工作时间会导致下一个工作时间开始或上一天结束。</span><span class="yiyi-st" id="yiyi-388">与其他偏移不同，<code class="docutils literal"><span class="pre">BusinessHour.rollforward</span></code>可根据定义从<code class="docutils literal"><span class="pre">apply</span></code>输出不同的结果。</span></p>
<p><span class="yiyi-st" id="yiyi-389">这是因为一天的营业时间结束等于下一天的营业时间开始。</span><span class="yiyi-st" id="yiyi-390">例如，在默认营业时间（9:00  -  17:00）之间，<code class="docutils literal"><span class="pre">2014-08-01</span> <span class="pre">17:00之间没有间隔（0分钟） t2&gt;</span></code>和<code class="docutils literal"><span class="pre">2014-08-04</span> <span class="pre">09:00</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># This adjusts a Timestamp to business hour edge</span>
<span class="gp">In [161]: </span><span class="n">BusinessHour</span><span class="p">()</span><span class="o">.</span><span class="n">rollback</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-02 15:00&apos;</span><span class="p">))</span>
<span class="gr">Out[161]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-01 17:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [162]: </span><span class="n">BusinessHour</span><span class="p">()</span><span class="o">.</span><span class="n">rollforward</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-02 15:00&apos;</span><span class="p">))</span>
<span class="gr">Out[162]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-04 09:00:00&apos;</span><span class="p">)</span>

<span class="c"># It is the same as BusinessHour().apply(pd.Timestamp(&apos;2014-08-01 17:00&apos;)).</span>
<span class="c"># And it is the same as BusinessHour().apply(pd.Timestamp(&apos;2014-08-04 09:00&apos;))</span>
<span class="gp">In [163]: </span><span class="n">BusinessHour</span><span class="p">()</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-02 15:00&apos;</span><span class="p">))</span>
<span class="gr">Out[163]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-04 10:00:00&apos;</span><span class="p">)</span>

<span class="c"># BusinessDay results (for reference)</span>
<span class="gp">In [164]: </span><span class="n">BusinessHour</span><span class="p">()</span><span class="o">.</span><span class="n">rollforward</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-02&apos;</span><span class="p">))</span>
<span class="gr">Out[164]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-04 09:00:00&apos;</span><span class="p">)</span>

<span class="c"># It is the same as BusinessDay().apply(pd.Timestamp(&apos;2014-08-01&apos;))</span>
<span class="c"># The result is the same as rollworward because BusinessDay never overlap.</span>
<span class="gp">In [165]: </span><span class="n">BusinessHour</span><span class="p">()</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-02&apos;</span><span class="p">))</span>
<span class="gr">Out[165]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-08-04 10:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-391"><code class="docutils literal"><span class="pre">BusinessHour</span></code>将星期六和星期日视为假日。</span><span class="yiyi-st" id="yiyi-392">要使用任意假日，您可以使用<code class="docutils literal"><span class="pre">CustomBusinessHour</span></code>偏移量，请参阅<a class="reference internal" href="#timeseries-custombusinesshour"><span class="std std-ref">Custom Business Hour</span></a>：</span></p>
</div>
<div class="section" id="custom-business-hour">
<span id="timeseries-custombusinesshour"></span><h3><span class="yiyi-st" id="yiyi-393">Custom Business Hour</span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-394"><span class="versionmodified">版本0.18.1中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-395"><code class="docutils literal"><span class="pre">CustomBusinessHour</span></code>是<code class="docutils literal"><span class="pre">BusinessHour</span></code>和<code class="docutils literal"><span class="pre">CustomBusinessDay</span></code>的混合体，允许您指定任意假日。</span><span class="yiyi-st" id="yiyi-396"><code class="docutils literal"><span class="pre">CustomBusinessHour</span></code>与<code class="docutils literal"><span class="pre">BusinessHour</span></code>的工作方式相同，只是它跳过指定的自定义假日。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [166]: </span><span class="kn">from</span> <span class="nn">pandas.tseries.holiday</span> <span class="kn">import</span> <span class="n">USFederalHolidayCalendar</span>

<span class="gp">In [167]: </span><span class="n">bhour_us</span> <span class="o">=</span> <span class="n">CustomBusinessHour</span><span class="p">(</span><span class="n">calendar</span><span class="o">=</span><span class="n">USFederalHolidayCalendar</span><span class="p">())</span>

<span class="c"># Friday before MLK Day</span>
<span class="gp">In [168]: </span><span class="n">dt</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2014</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span>

<span class="gp">In [169]: </span><span class="n">dt</span> <span class="o">+</span> <span class="n">bhour_us</span>
<span class="gr">Out[169]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-17 16:00:00&apos;</span><span class="p">)</span>

<span class="c"># Tuesday after MLK Day (Monday is skipped because it&apos;s a holiday)</span>
<span class="gp">In [170]: </span><span class="n">dt</span> <span class="o">+</span> <span class="n">bhour_us</span> <span class="o">*</span> <span class="mi">2</span>
<span class="gr">Out[170]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-21 09:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-397">您可以使用由<code class="docutils literal"><span class="pre">BusinessHour</span></code>和<code class="docutils literal"><span class="pre">CustomBusinessDay</span></code>支持的关键字参数。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [171]: </span><span class="n">bhour_mon</span> <span class="o">=</span> <span class="n">CustomBusinessHour</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="s1">&apos;10:00&apos;</span><span class="p">,</span> <span class="n">weekmask</span><span class="o">=</span><span class="s1">&apos;Tue Wed Thu Fri&apos;</span><span class="p">)</span>

<span class="c"># Monday is skipped because it&apos;s a holiday, business hour starts from 10:00</span>
<span class="gp">In [172]: </span><span class="n">dt</span> <span class="o">+</span> <span class="n">bhour_mon</span> <span class="o">*</span> <span class="mi">2</span>
<span class="gr">Out[172]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-21 10:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="offset-aliases">
<span id="timeseries-offset-aliases"></span><h3><span class="yiyi-st" id="yiyi-398">Offset Aliases</span></h3>
<p><span class="yiyi-st" id="yiyi-399">许多字符串别名被赋予有用的公共时间系列频率。</span><span class="yiyi-st" id="yiyi-400">我们将这些别名称为<em>偏移别名</em>（在v0.8.0之前称为<em>时间规则</em>）。</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="13%">
<col width="87%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-401">别名</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-402">描述</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-403">B</span></td>
<td><span class="yiyi-st" id="yiyi-404">工作日频率</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-405">C</span></td>
<td><span class="yiyi-st" id="yiyi-406">定制工作日频率（实验）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-407">D</span></td>
<td><span class="yiyi-st" id="yiyi-408">日历日频率</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-409">W</span></td>
<td><span class="yiyi-st" id="yiyi-410">每周频率</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-411">M</span></td>
<td><span class="yiyi-st" id="yiyi-412">月结束频率</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-413">SM</span></td>
<td><span class="yiyi-st" id="yiyi-414">半月结束频率（15个月和月末）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-415">BM</span></td>
<td><span class="yiyi-st" id="yiyi-416">业务月末频率</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-417">CBM</span></td>
<td><span class="yiyi-st" id="yiyi-418">定制业务月末频率</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-419">MS</span></td>
<td><span class="yiyi-st" id="yiyi-420">月起始频率</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-421">SMS</span></td>
<td><span class="yiyi-st" id="yiyi-422">半月起始频率（第1和15）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-423">BMS</span></td>
<td><span class="yiyi-st" id="yiyi-424">业务月开始频率</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-425">CBMS</span></td>
<td><span class="yiyi-st" id="yiyi-426">定制商业月份开始频率</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-427">Q</span></td>
<td><span class="yiyi-st" id="yiyi-428">四分频结束频率</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-429">BQ</span></td>
<td><span class="yiyi-st" id="yiyi-430">业务四分之一频率</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-431">QS</span></td>
<td><span class="yiyi-st" id="yiyi-432">四分频启动频率</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-433">BQS</span></td>
<td><span class="yiyi-st" id="yiyi-434">业务季开始频率</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-435">A</span></td>
<td><span class="yiyi-st" id="yiyi-436">年结束频率</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-437">BA</span></td>
<td><span class="yiyi-st" id="yiyi-438">业务年结束频率</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-439">AS</span></td>
<td><span class="yiyi-st" id="yiyi-440">年起始频率</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-441">BAS</span></td>
<td><span class="yiyi-st" id="yiyi-442">业务年开始频率</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-443">BH</span></td>
<td><span class="yiyi-st" id="yiyi-444">工作时间频率</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-445">H</span></td>
<td><span class="yiyi-st" id="yiyi-446">每小时频率</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-447">T，min</span></td>
<td><span class="yiyi-st" id="yiyi-448">分钟频率</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-449">S</span></td>
<td><span class="yiyi-st" id="yiyi-450">次频</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-451">L，ms</span></td>
<td><span class="yiyi-st" id="yiyi-452">毫秒</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-453">U，us</span></td>
<td><span class="yiyi-st" id="yiyi-454">微秒</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-455">N</span></td>
<td><span class="yiyi-st" id="yiyi-456">纳秒</span></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="combining-aliases">
<h3><span class="yiyi-st" id="yiyi-457">Combining Aliases</span></h3>
<p><span class="yiyi-st" id="yiyi-458">正如我们之前所看到的，别名和偏移实例在大多数函数中是可替换的：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [173]: </span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;B&apos;</span><span class="p">)</span>
<span class="gr">Out[173]: </span>
<span class="go">DatetimeIndex([&apos;2011-01-03&apos;, &apos;2011-01-04&apos;, &apos;2011-01-05&apos;, &apos;2011-01-06&apos;,</span>
<span class="go">               &apos;2011-01-07&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;B&apos;)</span>

<span class="gp">In [174]: </span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="n">BDay</span><span class="p">())</span>
<span class="gr">Out[174]: </span>
<span class="go">DatetimeIndex([&apos;2011-01-03&apos;, &apos;2011-01-04&apos;, &apos;2011-01-05&apos;, &apos;2011-01-06&apos;,</span>
<span class="go">               &apos;2011-01-07&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;B&apos;)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-459">您可以将日和期内抵消结合在一起：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [175]: </span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;2h20min&apos;</span><span class="p">)</span>
<span class="gr">Out[175]: </span>
<span class="go">DatetimeIndex([&apos;2011-01-01 00:00:00&apos;, &apos;2011-01-01 02:20:00&apos;,</span>
<span class="go">               &apos;2011-01-01 04:40:00&apos;, &apos;2011-01-01 07:00:00&apos;,</span>
<span class="go">               &apos;2011-01-01 09:20:00&apos;, &apos;2011-01-01 11:40:00&apos;,</span>
<span class="go">               &apos;2011-01-01 14:00:00&apos;, &apos;2011-01-01 16:20:00&apos;,</span>
<span class="go">               &apos;2011-01-01 18:40:00&apos;, &apos;2011-01-01 21:00:00&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;140T&apos;)</span>

<span class="gp">In [176]: </span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;1D10U&apos;</span><span class="p">)</span>
<span class="gr">Out[176]: </span>
<span class="go">DatetimeIndex([       &apos;2011-01-01 00:00:00&apos;, &apos;2011-01-02 00:00:00.000010&apos;,</span>
<span class="go">               &apos;2011-01-03 00:00:00.000020&apos;, &apos;2011-01-04 00:00:00.000030&apos;,</span>
<span class="go">               &apos;2011-01-05 00:00:00.000040&apos;, &apos;2011-01-06 00:00:00.000050&apos;,</span>
<span class="go">               &apos;2011-01-07 00:00:00.000060&apos;, &apos;2011-01-08 00:00:00.000070&apos;,</span>
<span class="go">               &apos;2011-01-09 00:00:00.000080&apos;, &apos;2011-01-10 00:00:00.000090&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;86400000010U&apos;)</span>
</pre></div>
</div>
</div>
<div class="section" id="anchored-offsets">
<h3><span class="yiyi-st" id="yiyi-460">Anchored Offsets</span></h3>
<p><span class="yiyi-st" id="yiyi-461">对于某些频率，您可以指定锚定后缀：</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="13%">
<col width="87%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-462">别名</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-463">描述</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-464">W-SUN</span></td>
<td><span class="yiyi-st" id="yiyi-465">每周频率（星期日）。</span><span class="yiyi-st" id="yiyi-466">与“W”相同</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-467">W-MON</span></td>
<td><span class="yiyi-st" id="yiyi-468">每周频率（星期一）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-469">W-TUE</span></td>
<td><span class="yiyi-st" id="yiyi-470">每周频率</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-471">W-WED</span></td>
<td><span class="yiyi-st" id="yiyi-472">每周频率</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-473">W-THU</span></td>
<td><span class="yiyi-st" id="yiyi-474">每周频率（thursdays）</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-475">W-FRI</span></td>
<td><span class="yiyi-st" id="yiyi-476">每周频率（星期五）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-477">W-SAT</span></td>
<td><span class="yiyi-st" id="yiyi-478">每周频率（saturdays）</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-479">（B）Q（S）-DEC</span></td>
<td><span class="yiyi-st" id="yiyi-480">季度频率，年度在12月结束。</span><span class="yiyi-st" id="yiyi-481">与“Q”相同</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-482">（B）Q（S）-JAN</span></td>
<td><span class="yiyi-st" id="yiyi-483">季度频率，年度在1月结束</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-484">（B）Q（S）-FEB</span></td>
<td><span class="yiyi-st" id="yiyi-485">季度频率，年度在2月结束</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-486">（B）Q（S）-MAR</span></td>
<td><span class="yiyi-st" id="yiyi-487">季度频率，年度在3月结束</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-488">（B）Q（S）-APR</span></td>
<td><span class="yiyi-st" id="yiyi-489">季度频率，年度在4月结束</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-490">（B）Q（S）-MAY</span></td>
<td><span class="yiyi-st" id="yiyi-491">季度频率，年度在5月结束</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-492">（B）Q（S）-JUN</span></td>
<td><span class="yiyi-st" id="yiyi-493">季度频率，年度在6月结束</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-494">（B）Q（S）-JUL</span></td>
<td><span class="yiyi-st" id="yiyi-495">季度频率，年度在7月结束</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-496">（B）Q（S）-AUG</span></td>
<td><span class="yiyi-st" id="yiyi-497">季度频率，年度在8月结束</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-498">（B）Q（S）-SEP</span></td>
<td><span class="yiyi-st" id="yiyi-499">季度频率，年度在9月结束</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-500">（B）Q（S）-OCT</span></td>
<td><span class="yiyi-st" id="yiyi-501">季度频率，年度在10月结束</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-502">（B）Q（S）-NOV</span></td>
<td><span class="yiyi-st" id="yiyi-503">季度频率，年度在11月结束</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-504">（B）A（S）-DEC</span></td>
<td><span class="yiyi-st" id="yiyi-505">年度频率，定于12月底。</span><span class="yiyi-st" id="yiyi-506">与“A”相同</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-507">（B）A（S）-JAN</span></td>
<td><span class="yiyi-st" id="yiyi-508">年度频率，定于1月底</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-509">（B）A（S）-FEB</span></td>
<td><span class="yiyi-st" id="yiyi-510">年度频率，定于2月底</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-511">（B）A（S）-MAR</span></td>
<td><span class="yiyi-st" id="yiyi-512">年度频率，定于3月底</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-513">（B）A（S）-APR</span></td>
<td><span class="yiyi-st" id="yiyi-514">年度频率，定于4月底</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-515">（B）A（S）-MAY</span></td>
<td><span class="yiyi-st" id="yiyi-516">年度频率，定于5月底</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-517">（B）A（S）-JUN</span></td>
<td><span class="yiyi-st" id="yiyi-518">年度频率，定于6月底</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-519">（B）A（S）-JUL</span></td>
<td><span class="yiyi-st" id="yiyi-520">年度频率，定于7月底</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-521">（B）A（S）-AUG</span></td>
<td><span class="yiyi-st" id="yiyi-522">年度频率，定于8月底</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-523">（B）A（S）-SEP</span></td>
<td><span class="yiyi-st" id="yiyi-524">年度频率，定于9月底</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-525">（B）A（S）-OCT</span></td>
<td><span class="yiyi-st" id="yiyi-526">年度频率，定于10月底</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-527">（B）A（S）-NOV</span></td>
<td><span class="yiyi-st" id="yiyi-528">年度频率，定于11月底</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-529">这些可以用作<code class="docutils literal"><span class="pre">date_range</span></code>，<code class="docutils literal"><span class="pre">bdate_range</span></code>，<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>的构造函数以及pandas中其他各种与时间序列相关的函数的参数。</span></p>
</div>
<div class="section" id="anchored-offset-semantics">
<h3><span class="yiyi-st" id="yiyi-530">Anchored Offset Semantics</span></h3>
<p><span class="yiyi-st" id="yiyi-531">对于固定在特定频率（<code class="docutils literal"><span class="pre">MonthEnd</span></code>，<code class="docutils literal"><span class="pre">MonthBegin</span></code>，<code class="docutils literal"><span class="pre">WeekEnd</span></code>等）开始或结束的偏移量，以下规则适用于滚动前进和后退。</span></p>
<p><span class="yiyi-st" id="yiyi-532">当<code class="docutils literal"><span class="pre">n</span></code>不为0时，如果给定日期不在锚点上，则它会捕捉到下一个（上一个）锚点，并移动<code class="docutils literal"><span class="pre">|n|-1</span></code>向前或向后步进。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [177]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-02&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">MonthBegin</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[177]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-02-01 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [178]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-02&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">MonthEnd</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[178]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-31 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [179]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-02&apos;</span><span class="p">)</span> <span class="o">-</span> <span class="n">MonthBegin</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[179]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-01 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [180]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-02&apos;</span><span class="p">)</span> <span class="o">-</span> <span class="n">MonthEnd</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[180]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2013-12-31 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [181]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-02&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">MonthBegin</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="gr">Out[181]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-05-01 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [182]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-02&apos;</span><span class="p">)</span> <span class="o">-</span> <span class="n">MonthBegin</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="gr">Out[182]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2013-10-01 00:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-533">如果给定日期<em>是</em>在锚点上，则向前或向后移动<code class="docutils literal"><span class="pre">|n|</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [183]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-01&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">MonthBegin</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[183]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-02-01 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [184]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-31&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">MonthEnd</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[184]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-02-28 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [185]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-01&apos;</span><span class="p">)</span> <span class="o">-</span> <span class="n">MonthBegin</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[185]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2013-12-01 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [186]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-31&apos;</span><span class="p">)</span> <span class="o">-</span> <span class="n">MonthEnd</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[186]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2013-12-31 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [187]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-01&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">MonthBegin</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="gr">Out[187]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-05-01 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [188]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-31&apos;</span><span class="p">)</span> <span class="o">-</span> <span class="n">MonthBegin</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="gr">Out[188]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2013-10-01 00:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-534">对于<code class="docutils literal"><span class="pre">n=0</span></code>的情况，如果在锚点上，则不移动日期，否则将向前滚动到下一个锚点。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [189]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-02&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">MonthBegin</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[189]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-02-01 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [190]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-02&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">MonthEnd</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[190]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-31 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [191]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-01&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">MonthBegin</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[191]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-01 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [192]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-31&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">MonthEnd</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[192]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2014-01-31 00:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="holidays-holiday-calendars">
<span id="timeseries-holiday"></span><h3><span class="yiyi-st" id="yiyi-535">Holidays / Holiday Calendars</span></h3>
<p><span class="yiyi-st" id="yiyi-536">假日和日历提供了一种简单的方法来定义要与<code class="docutils literal"><span class="pre">CustomBusinessDay</span></code>配合使用的假日规则，或在需要预定义假日集的其他分析中。</span><span class="yiyi-st" id="yiyi-537"><code class="docutils literal"><span class="pre">AbstractHolidayCalendar</span></code>类提供了返回节假日列表所需的所有方法，并且只需要在特定假日日历类中定义<code class="docutils literal"><span class="pre">rules</span></code>。</span><span class="yiyi-st" id="yiyi-538">此外，<code class="docutils literal"><span class="pre">start_date</span></code>和<code class="docutils literal"><span class="pre">end_date</span></code>类别属性决定生成哪些日期范围的假日。</span><span class="yiyi-st" id="yiyi-539">这些应该在<code class="docutils literal"><span class="pre">AbstractHolidayCalendar</span></code>类上覆盖，以将范围应用于所有日历子类。</span><span class="yiyi-st" id="yiyi-540"><code class="docutils literal"><span class="pre">USFederalHolidayCalendar</span></code>是唯一存在的日历，主要用作开发其他日历的示例。</span></p>
<p><span class="yiyi-st" id="yiyi-541">对于在固定日期（例如，美国纪念日或7月4日）发生的假日，遵守规则确定当该假日落在周末或某些其他非观察日时，何时观察该假日。</span><span class="yiyi-st" id="yiyi-542">定义的遵守规则是：</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="18%">
<col width="82%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-543">规则</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-544">描述</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-545">nearest_workday</span></td>
<td><span class="yiyi-st" id="yiyi-546">周六至周五，周日至周一</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-547">sunday_to_monday</span></td>
<td><span class="yiyi-st" id="yiyi-548">周日移至下周一</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-549">next_monday_or_tuesday</span></td>
<td><span class="yiyi-st" id="yiyi-550">星期六到星期一和星期日/星期一到星期二</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-551">previous_friday</span></td>
<td><span class="yiyi-st" id="yiyi-552">将星期六和星期日移动到上一个星期五“</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-553">next_monday</span></td>
<td><span class="yiyi-st" id="yiyi-554">将星期六和星期日移动到下一个星期一</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-555">定义假日和假日日历的示例：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [193]: </span><span class="kn">from</span> <span class="nn">pandas.tseries.holiday</span> <span class="kn">import</span> <span class="n">Holiday</span><span class="p">,</span> <span class="n">USMemorialDay</span><span class="p">,</span>\
<span class="gp">   .....:</span>     <span class="n">AbstractHolidayCalendar</span><span class="p">,</span> <span class="n">nearest_workday</span><span class="p">,</span> <span class="n">MO</span>
<span class="gp">   .....:</span> 

<span class="gp">In [194]: </span><span class="k">class</span> <span class="nc">ExampleCalendar</span><span class="p">(</span><span class="n">AbstractHolidayCalendar</span><span class="p">):</span>
<span class="gp">   .....:</span>     <span class="n">rules</span> <span class="o">=</span> <span class="p">[</span>
<span class="gp">   .....:</span>         <span class="n">USMemorialDay</span><span class="p">,</span>
<span class="gp">   .....:</span>         <span class="n">Holiday</span><span class="p">(</span><span class="s1">&apos;July 4th&apos;</span><span class="p">,</span> <span class="n">month</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">observance</span><span class="o">=</span><span class="n">nearest_workday</span><span class="p">),</span>
<span class="gp">   .....:</span>         <span class="n">Holiday</span><span class="p">(</span><span class="s1">&apos;Columbus Day&apos;</span><span class="p">,</span> <span class="n">month</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="gp">   .....:</span>             <span class="n">offset</span><span class="o">=</span><span class="n">DateOffset</span><span class="p">(</span><span class="n">weekday</span><span class="o">=</span><span class="n">MO</span><span class="p">(</span><span class="mi">2</span><span class="p">))),</span> <span class="c1">#same as 2*Week(weekday=2)</span>
<span class="gp">   .....:</span>         <span class="p">]</span>
<span class="gp">   .....:</span> 

<span class="gp">In [195]: </span><span class="n">cal</span> <span class="o">=</span> <span class="n">ExampleCalendar</span><span class="p">()</span>

<span class="gp">In [196]: </span><span class="n">cal</span><span class="o">.</span><span class="n">holidays</span><span class="p">(</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">))</span>
<span class="gr">Out[196]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2012-05-28&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-07-04&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-10-08&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-556">使用此日历，创建索引或进行偏移算术会跳过周末和假日（即阵亡将士纪念日/ 7月4日）。</span><span class="yiyi-st" id="yiyi-557">例如，以下使用<code class="docutils literal"><span class="pre">ExampleCalendar</span></code>定义自定义工作日偏移量。</span><span class="yiyi-st" id="yiyi-558">像任何其他偏移一样，它可以用于创建<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>或添加到<code class="docutils literal"><span class="pre">datetime</span></code>或<code class="docutils literal"><span class="pre">Timestamp</span></code>对象。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [197]: </span><span class="kn">from</span> <span class="nn">pandas.tseries.offsets</span> <span class="kn">import</span> <span class="n">CDay</span>

<span class="gp">In [198]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DatetimeIndex</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="s1">&apos;7/1/2012&apos;</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">&apos;7/10/2012&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>     <span class="n">freq</span><span class="o">=</span><span class="n">CDay</span><span class="p">(</span><span class="n">calendar</span><span class="o">=</span><span class="n">cal</span><span class="p">))</span><span class="o">.</span><span class="n">to_pydatetime</span><span class="p">()</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[198]: </span>
<span class="go">array([datetime.datetime(2012, 7, 2, 0, 0),</span>
<span class="go">       datetime.datetime(2012, 7, 3, 0, 0),</span>
<span class="go">       datetime.datetime(2012, 7, 5, 0, 0),</span>
<span class="go">       datetime.datetime(2012, 7, 6, 0, 0),</span>
<span class="go">       datetime.datetime(2012, 7, 9, 0, 0),</span>
<span class="go">       datetime.datetime(2012, 7, 10, 0, 0)], dtype=object)</span>

<span class="gp">In [199]: </span><span class="n">offset</span> <span class="o">=</span> <span class="n">CustomBusinessDay</span><span class="p">(</span><span class="n">calendar</span><span class="o">=</span><span class="n">cal</span><span class="p">)</span>

<span class="gp">In [200]: </span><span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">25</span><span class="p">)</span> <span class="o">+</span> <span class="n">offset</span>
<span class="gr">Out[200]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-05-29 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [201]: </span><span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="n">offset</span>
<span class="gr">Out[201]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-07-05 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [202]: </span><span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">offset</span>
<span class="gr">Out[202]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-07-06 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [203]: </span><span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span> <span class="o">+</span> <span class="n">offset</span>
<span class="gr">Out[203]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-07-09 00:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-559">范围由<code class="docutils literal"><span class="pre">AbstractHolidayCalendar</span></code>的<code class="docutils literal"><span class="pre">start_date</span></code>和<code class="docutils literal"><span class="pre">end_date</span></code>类属性定义。</span><span class="yiyi-st" id="yiyi-560">默认值如下。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [204]: </span><span class="n">AbstractHolidayCalendar</span><span class="o">.</span><span class="n">start_date</span>
<span class="gr">Out[204]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;1970-01-01 00:00:00&apos;</span><span class="p">)</span>

<span class="gp">In [205]: </span><span class="n">AbstractHolidayCalendar</span><span class="o">.</span><span class="n">end_date</span>
<span class="gr">Out[205]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2030-12-31 00:00:00&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-561">可以通过将属性设置为datetime / Timestamp / string来覆盖这些日期。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [206]: </span><span class="n">AbstractHolidayCalendar</span><span class="o">.</span><span class="n">start_date</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

<span class="gp">In [207]: </span><span class="n">AbstractHolidayCalendar</span><span class="o">.</span><span class="n">end_date</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">)</span>

<span class="gp">In [208]: </span><span class="n">cal</span><span class="o">.</span><span class="n">holidays</span><span class="p">()</span>
<span class="gr">Out[208]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2012-05-28&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-07-04&apos;</span><span class="p">,</span> <span class="s1">&apos;2012-10-08&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-562">每个日历类都可以通过名称使用<code class="docutils literal"><span class="pre">get_calendar</span></code>函数访问，该函数返回假日类实例。</span><span class="yiyi-st" id="yiyi-563">任何导入的日历类都将自动通过此功能可用。</span><span class="yiyi-st" id="yiyi-564">此外，<code class="docutils literal"><span class="pre">HolidayCalendarFactory</span></code>提供了一个简单的界面，用于创建日历，这些日历是具有其他规则的日历或日历的组合。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [209]: </span><span class="kn">from</span> <span class="nn">pandas.tseries.holiday</span> <span class="kn">import</span> <span class="n">get_calendar</span><span class="p">,</span> <span class="n">HolidayCalendarFactory</span><span class="p">,</span>\
<span class="gp">   .....:</span>     <span class="n">USLaborDay</span>
<span class="gp">   .....:</span> 

<span class="gp">In [210]: </span><span class="n">cal</span> <span class="o">=</span> <span class="n">get_calendar</span><span class="p">(</span><span class="s1">&apos;ExampleCalendar&apos;</span><span class="p">)</span>

<span class="gp">In [211]: </span><span class="n">cal</span><span class="o">.</span><span class="n">rules</span>
<span class="gr">Out[211]: </span>
<span class="go">[Holiday: MemorialDay (month=5, day=31, offset=&lt;DateOffset: kwds={&apos;weekday&apos;: MO(-1)}&gt;),</span>
<span class="go"> Holiday: July 4th (month=7, day=4, observance=&lt;function nearest_workday at 0x7ff271135aa0&gt;),</span>
<span class="go"> Holiday: Columbus Day (month=10, day=1, offset=&lt;DateOffset: kwds={&apos;weekday&apos;: MO(+2)}&gt;)]</span>

<span class="gp">In [212]: </span><span class="n">new_cal</span> <span class="o">=</span> <span class="n">HolidayCalendarFactory</span><span class="p">(</span><span class="s1">&apos;NewExampleCalendar&apos;</span><span class="p">,</span> <span class="n">cal</span><span class="p">,</span> <span class="n">USLaborDay</span><span class="p">)</span>

<span class="gp">In [213]: </span><span class="n">new_cal</span><span class="o">.</span><span class="n">rules</span>
<span class="gr">Out[213]: </span>
<span class="go">[Holiday: Labor Day (month=9, day=1, offset=&lt;DateOffset: kwds={&apos;weekday&apos;: MO(+1)}&gt;),</span>
<span class="go"> Holiday: Columbus Day (month=10, day=1, offset=&lt;DateOffset: kwds={&apos;weekday&apos;: MO(+2)}&gt;),</span>
<span class="go"> Holiday: July 4th (month=7, day=4, observance=&lt;function nearest_workday at 0x7ff271135aa0&gt;),</span>
<span class="go"> Holiday: MemorialDay (month=5, day=31, offset=&lt;DateOffset: kwds={&apos;weekday&apos;: MO(-1)}&gt;)]</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="time-series-related-instance-methods">
<span id="timeseries-advanced-datetime"></span><h2><span class="yiyi-st" id="yiyi-565">Time series-related instance methods</span></h2>
<div class="section" id="shifting-lagging">
<h3><span class="yiyi-st" id="yiyi-566">Shifting / lagging</span></h3>
<p><span class="yiyi-st" id="yiyi-567">可能想要将时间序列中的值向后和向前移动<em>shift</em>或<em>lag</em>。</span><span class="yiyi-st" id="yiyi-568">其方法是<code class="docutils literal"><span class="pre">shift</span></code>，它可用于所有的pandas对象。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [214]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">ts</span><span class="p">[:</span><span class="mi">5</span><span class="p">]</span>

<span class="gp">In [215]: </span><span class="n">ts</span><span class="o">.</span><span class="n">shift</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[215]: </span>
<span class="go">2011-01-31         NaN</span>
<span class="go">2011-02-28   -1.281247</span>
<span class="go">2011-03-31   -0.727707</span>
<span class="go">2011-04-29   -0.121306</span>
<span class="go">2011-05-31   -0.097883</span>
<span class="go">Freq: BM, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-569">The shift method accepts an <code class="docutils literal"><span class="pre">freq</span></code> argument which can accept a <code class="docutils literal"><span class="pre">DateOffset</span></code> class or other <code class="docutils literal"><span class="pre">timedelta</span></code>-like object or also a <a class="reference internal" href="#timeseries-offset-aliases"><span class="std std-ref">offset alias</span></a>:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [216]: </span><span class="n">ts</span><span class="o">.</span><span class="n">shift</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="n">offsets</span><span class="o">.</span><span class="n">BDay</span><span class="p">())</span>
<span class="gr">Out[216]: </span>
<span class="go">2011-02-07   -1.281247</span>
<span class="go">2011-03-07   -0.727707</span>
<span class="go">2011-04-07   -0.121306</span>
<span class="go">2011-05-06   -0.097883</span>
<span class="go">2011-06-07    0.695775</span>
<span class="go">dtype: float64</span>

<span class="gp">In [217]: </span><span class="n">ts</span><span class="o">.</span><span class="n">shift</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;BM&apos;</span><span class="p">)</span>
<span class="gr">Out[217]: </span>
<span class="go">2011-06-30   -1.281247</span>
<span class="go">2011-07-29   -0.727707</span>
<span class="go">2011-08-31   -0.121306</span>
<span class="go">2011-09-30   -0.097883</span>
<span class="go">2011-10-31    0.695775</span>
<span class="go">Freq: BM, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-570"><code class="docutils literal"><span class="pre">DataFrame</span></code>和<code class="docutils literal"><span class="pre">Series</span></code>对象还有一个<code class="docutils literal"><span class="pre">tshift</span></code>方便方法，可以更改数据和索引的所有日期索引指定的偏移量：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [218]: </span><span class="n">ts</span><span class="o">.</span><span class="n">tshift</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>
<span class="gr">Out[218]: </span>
<span class="go">2011-02-05   -1.281247</span>
<span class="go">2011-03-05   -0.727707</span>
<span class="go">2011-04-05   -0.121306</span>
<span class="go">2011-05-04   -0.097883</span>
<span class="go">2011-06-05    0.695775</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-571">请注意，对于<code class="docutils literal"><span class="pre">tshift</span></code>，由于数据未重新对齐，前导项不再为NaN。</span></p>
</div>
<div class="section" id="frequency-conversion">
<h3><span class="yiyi-st" id="yiyi-572">Frequency conversion</span></h3>
<p><span class="yiyi-st" id="yiyi-573">改变频率的主要功能是<code class="docutils literal"><span class="pre">asfreq</span></code>功能。</span><span class="yiyi-st" id="yiyi-574">对于<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>，这基本上只是<code class="docutils literal"><span class="pre">reindex</span></code>的一个简单而方便的包装器，它生成<code class="docutils literal"><span class="pre">date_range</span></code>并调用<code class="docutils literal"><span class="pre">reindex</span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [219]: </span><span class="n">dr</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/1/2010&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="mi">3</span> <span class="o">*</span> <span class="n">offsets</span><span class="o">.</span><span class="n">BDay</span><span class="p">())</span>

<span class="gp">In [220]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">dr</span><span class="p">)</span>

<span class="gp">In [221]: </span><span class="n">ts</span>
<span class="gr">Out[221]: </span>
<span class="go">2010-01-01    0.532005</span>
<span class="go">2010-01-06    0.544874</span>
<span class="go">2010-01-11   -1.001788</span>
<span class="go">Freq: 3B, dtype: float64</span>

<span class="gp">In [222]: </span><span class="n">ts</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="n">BDay</span><span class="p">())</span>
<span class="gr">Out[222]: </span>
<span class="go">2010-01-01    0.532005</span>
<span class="go">2010-01-04         NaN</span>
<span class="go">2010-01-05         NaN</span>
<span class="go">2010-01-06    0.544874</span>
<span class="go">2010-01-07         NaN</span>
<span class="go">2010-01-08         NaN</span>
<span class="go">2010-01-11   -1.001788</span>
<span class="go">Freq: B, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-575"><code class="docutils literal"><span class="pre">asfreq</span></code>提供了进一步的便利，因此您可以为频率转换后可能出现的任何间隙指定插值方法</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [223]: </span><span class="n">ts</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="n">BDay</span><span class="p">(),</span> <span class="n">method</span><span class="o">=</span><span class="s1">&apos;pad&apos;</span><span class="p">)</span>
<span class="gr">Out[223]: </span>
<span class="go">2010-01-01    0.532005</span>
<span class="go">2010-01-04    0.532005</span>
<span class="go">2010-01-05    0.532005</span>
<span class="go">2010-01-06    0.544874</span>
<span class="go">2010-01-07    0.544874</span>
<span class="go">2010-01-08    0.544874</span>
<span class="go">2010-01-11   -1.001788</span>
<span class="go">Freq: B, dtype: float64</span>
</pre></div>
</div>
</div>
<div class="section" id="filling-forward-backward">
<h3><span class="yiyi-st" id="yiyi-576">Filling forward / backward</span></h3>
<p><span class="yiyi-st" id="yiyi-577">与<code class="docutils literal"><span class="pre">asfreq</span></code>和<code class="docutils literal"><span class="pre">reindex</span></code>相关的是<a class="reference internal" href="missing_data.html#missing-data-fillna"><span class="std std-ref">missing data section</span></a>中记录的<code class="docutils literal"><span class="pre">fillna</span></code>函数。</span></p>
</div>
<div class="section" id="converting-to-python-datetimes">
<h3><span class="yiyi-st" id="yiyi-578">Converting to Python datetimes</span></h3>
<p><span class="yiyi-st" id="yiyi-579"><code class="docutils literal"><span class="pre">DatetimeIndex</span></code>可以使用<code class="docutils literal"><span class="pre">to_pydatetime</span></code>方法转换为Python本机datetime.datetime对象的数组。</span></p>
</div>
</div>
<div class="section" id="resampling">
<span id="timeseries-resampling"></span><h2><span class="yiyi-st" id="yiyi-580">Resampling</span></h2>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-581">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-582">到<code class="docutils literal"><span class="pre">.resample</span></code>的接口在0.18.0中已更改为更像groupby的，因此更灵活。</span><span class="yiyi-st" id="yiyi-583">有关与以前版本的比较，请参阅<a class="reference internal" href="whatsnew.html#whatsnew-0180-breaking-resample"><span class="std std-ref">whatsnew docs</span></a>。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-584">Pandas具有用于在频率转换期间执行重采样操作（例如，将第二数据转换为5分钟数据）的简单，强大和高效的功能。</span><span class="yiyi-st" id="yiyi-585">这在金融应用中是非常常见的，但不限于此。</span></p>
<p><span class="yiyi-st" id="yiyi-586"><code class="docutils literal"><span class="pre">.resample()</span></code>是基于时间的groupby，后面是每个组的缩减方法。</span></p>
<p><span class="yiyi-st" id="yiyi-587">从版本0.18.1开始，可以直接从DataFrameGroupBy对象使用<code class="docutils literal"><span class="pre">resample()</span></code>函数，请参阅<a class="reference internal" href="groupby.html#groupby-transform-window-resample"><span class="std std-ref">groupby docs</span></a>。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-588">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-589"><code class="docutils literal"><span class="pre">.resample()</span></code>类似于使用具有基于时间的偏移的<code class="docutils literal"><span class="pre">.rolling()</span></code>操作，参见讨论<cite>这里＆lt； stats.moments.ts-对比重采样＆gt；</cite></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-590">有关某些高级策略，请参阅一些<a class="reference internal" href="cookbook.html#cookbook-resample"><span class="std std-ref">cookbook examples</span></a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [224]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/1/2012&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;S&apos;</span><span class="p">)</span>

<span class="gp">In [225]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">500</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">rng</span><span class="p">)),</span> <span class="n">index</span><span class="o">=</span><span class="n">rng</span><span class="p">)</span>

<span class="gp">In [226]: </span><span class="n">ts</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;5Min&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gr">Out[226]: </span>
<span class="go">2012-01-01    24390</span>
<span class="go">Freq: 5T, dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-591"><code class="docutils literal"><span class="pre">resample</span></code>功能非常灵活，允许您指定许多不同的参数来控制频率转换和重采样操作。</span></p>
<p><span class="yiyi-st" id="yiyi-592"><code class="docutils literal"><span class="pre">how</span></code>参数可以是函数名或numpy数组函数，它接受数组并生成聚合值：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [227]: </span><span class="n">ts</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;5Min&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[227]: </span>
<span class="go">2012-01-01    243.9</span>
<span class="go">Freq: 5T, dtype: float64</span>

<span class="gp">In [228]: </span><span class="n">ts</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;5Min&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">ohlc</span><span class="p">()</span>
<span class="gr">Out[228]: </span>
<span class="go">            open  high  low  close</span>
<span class="go">2012-01-01   161   495    1    245</span>

<span class="gp">In [229]: </span><span class="n">ts</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;5Min&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
<span class="gr">Out[229]: </span>
<span class="go">2012-01-01    495</span>
<span class="go">Freq: 5T, dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-593">Any function available via <a class="reference internal" href="groupby.html#groupby-dispatch"><span class="std std-ref">dispatching</span></a> can be given to the <code class="docutils literal"><span class="pre">how</span></code> parameter by name, including <code class="docutils literal"><span class="pre">sum</span></code>, <code class="docutils literal"><span class="pre">mean</span></code>, <code class="docutils literal"><span class="pre">std</span></code>, <code class="docutils literal"><span class="pre">sem</span></code>, <code class="docutils literal"><span class="pre">max</span></code>, <code class="docutils literal"><span class="pre">min</span></code>, <code class="docutils literal"><span class="pre">median</span></code>, <code class="docutils literal"><span class="pre">first</span></code>, <code class="docutils literal"><span class="pre">last</span></code>, <code class="docutils literal"><span class="pre">ohlc</span></code>.</span></p>
<p><span class="yiyi-st" id="yiyi-594">对于下采样，可以将<code class="docutils literal"><span class="pre">closed</span></code>设置为“左”或“右”以指定间隔的哪个结束：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [230]: </span><span class="n">ts</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;5Min&apos;</span><span class="p">,</span> <span class="n">closed</span><span class="o">=</span><span class="s1">&apos;right&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[230]: </span>
<span class="go">2011-12-31 23:55:00    161.000000</span>
<span class="go">2012-01-01 00:00:00    244.737374</span>
<span class="go">Freq: 5T, dtype: float64</span>

<span class="gp">In [231]: </span><span class="n">ts</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;5Min&apos;</span><span class="p">,</span> <span class="n">closed</span><span class="o">=</span><span class="s1">&apos;left&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[231]: </span>
<span class="go">2012-01-01    243.9</span>
<span class="go">Freq: 5T, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-595">Parameters like <code class="docutils literal"><span class="pre">label</span></code> and <code class="docutils literal"><span class="pre">loffset</span></code> are used to manipulate the resulting labels. </span><span class="yiyi-st" id="yiyi-596"><code class="docutils literal"><span class="pre">label</span></code>指定结果是否标记有间隔的开始或结束。</span><span class="yiyi-st" id="yiyi-597"><code class="docutils literal"><span class="pre">loffset</span></code>对输出标签执行时间调整。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [232]: </span><span class="n">ts</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;5Min&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span> <span class="c1"># by default label=&apos;right&apos;</span>
<span class="gr">Out[232]: </span>
<span class="go">2012-01-01    243.9</span>
<span class="go">Freq: 5T, dtype: float64</span>

<span class="gp">In [233]: </span><span class="n">ts</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;5Min&apos;</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&apos;left&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[233]: </span>
<span class="go">2012-01-01    243.9</span>
<span class="go">Freq: 5T, dtype: float64</span>

<span class="gp">In [234]: </span><span class="n">ts</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;5Min&apos;</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&apos;left&apos;</span><span class="p">,</span> <span class="n">loffset</span><span class="o">=</span><span class="s1">&apos;1s&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[234]: </span>
<span class="go">2012-01-01 00:00:01    243.9</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-598"><code class="docutils literal"><span class="pre">axis</span></code>参数可以设置为0或1，并允许为DataFrame重新采样指定的轴。</span></p>
<p><span class="yiyi-st" id="yiyi-599"><code class="docutils literal"><span class="pre">kind</span></code>可以设置为“timestamp”或“period”，以将结果索引转换为/来自时间戳和时间跨度表示。</span><span class="yiyi-st" id="yiyi-600">默认情况下，<code class="docutils literal"><span class="pre">resample</span></code>保留输入表示。</span></p>
<p><span class="yiyi-st" id="yiyi-601"><code class="docutils literal"><span class="pre">convention</span></code>可以在重新采样周期数据（下面的详细信息）时设置为“开始”或“结束”。</span><span class="yiyi-st" id="yiyi-602">它指定如何将低频周期转换为较高频率周期。</span></p>
<div class="section" id="up-sampling">
<h3><span class="yiyi-st" id="yiyi-603">Up Sampling</span></h3>
<p><span class="yiyi-st" id="yiyi-604">对于上采样，您可以指定上采样的方式和<code class="docutils literal"><span class="pre">limit</span></code>参数，以在创建的间隙内插值：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># from secondly to every 250 milliseconds</span>
<span class="gp">In [235]: </span><span class="n">ts</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;250L&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">asfreq</span><span class="p">()</span>
<span class="gr">Out[235]: </span>
<span class="go">2012-01-01 00:00:00.000    161.0</span>
<span class="go">2012-01-01 00:00:00.250      NaN</span>
<span class="go">2012-01-01 00:00:00.500      NaN</span>
<span class="go">2012-01-01 00:00:00.750      NaN</span>
<span class="go">2012-01-01 00:00:01.000    199.0</span>
<span class="go">Freq: 250L, dtype: float64</span>

<span class="gp">In [236]: </span><span class="n">ts</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;250L&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">ffill</span><span class="p">()</span>
<span class="gr">Out[236]: </span>
<span class="go">2012-01-01 00:00:00.000    161</span>
<span class="go">2012-01-01 00:00:00.250    161</span>
<span class="go">2012-01-01 00:00:00.500    161</span>
<span class="go">2012-01-01 00:00:00.750    161</span>
<span class="go">2012-01-01 00:00:01.000    199</span>
<span class="go">Freq: 250L, dtype: int64</span>

<span class="gp">In [237]: </span><span class="n">ts</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;250L&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">ffill</span><span class="p">(</span><span class="n">limit</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[237]: </span>
<span class="go">2012-01-01 00:00:00.000    161.0</span>
<span class="go">2012-01-01 00:00:00.250    161.0</span>
<span class="go">2012-01-01 00:00:00.500    161.0</span>
<span class="go">2012-01-01 00:00:00.750      NaN</span>
<span class="go">2012-01-01 00:00:01.000    199.0</span>
<span class="go">Freq: 250L, dtype: float64</span>
</pre></div>
</div>
</div>
<div class="section" id="sparse-resampling">
<h3><span class="yiyi-st" id="yiyi-605">Sparse Resampling</span></h3>
<p><span class="yiyi-st" id="yiyi-606">稀疏时间序列是指相对于您要重新抽样的时间，点数较少的点。</span><span class="yiyi-st" id="yiyi-607">稀疏序列的稀疏上采样可能产生大量的中间值。</span><span class="yiyi-st" id="yiyi-608">当您不想使用方法来填充这些值时，例如<code class="docutils literal"><span class="pre">fill_method</span></code>是<code class="docutils literal"><span class="pre">None</span></code>，则中间值将用<code class="docutils literal"><span class="pre">NaN</span></code>填充。</span></p>
<p><span class="yiyi-st" id="yiyi-609">由于<code class="docutils literal"><span class="pre">resample</span></code>是基于时间的组，以下是一种方法，用于仅对不是全部<code class="docutils literal"><span class="pre">NaN</span></code>的组有效地重新取样，</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [238]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;2014-1-1&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">&apos;1s&apos;</span><span class="p">)</span>

<span class="gp">In [239]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">rng</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-610">如果我们想重新取样到系列的全部范围</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [240]: </span><span class="n">ts</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;3T&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gr">Out[240]: </span>
<span class="go">2014-01-01 00:00:00     0.0</span>
<span class="go">2014-01-01 00:03:00     NaN</span>
<span class="go">2014-01-01 00:06:00     NaN</span>
<span class="go">2014-01-01 00:09:00     NaN</span>
<span class="go">2014-01-01 00:12:00     NaN</span>
<span class="go">2014-01-01 00:15:00     NaN</span>
<span class="go">2014-01-01 00:18:00     NaN</span>
<span class="go">                       ... </span>
<span class="go">2014-04-09 23:42:00     NaN</span>
<span class="go">2014-04-09 23:45:00     NaN</span>
<span class="go">2014-04-09 23:48:00     NaN</span>
<span class="go">2014-04-09 23:51:00     NaN</span>
<span class="go">2014-04-09 23:54:00     NaN</span>
<span class="go">2014-04-09 23:57:00     NaN</span>
<span class="go">2014-04-10 00:00:00    99.0</span>
<span class="go">Freq: 3T, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-611">我们只能重新取样那些我们有分数的组，如下：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [241]: </span><span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">partial</span>

<span class="gp">In [242]: </span><span class="kn">from</span> <span class="nn">pandas.tseries.frequencies</span> <span class="kn">import</span> <span class="n">to_offset</span>

<span class="gp">In [243]: </span><span class="k">def</span> <span class="nf">round</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">freq</span><span class="p">):</span>
<span class="gp">   .....:</span>     <span class="n">freq</span> <span class="o">=</span> <span class="n">to_offset</span><span class="p">(</span><span class="n">freq</span><span class="p">)</span>
<span class="gp">   .....:</span>     <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">((</span><span class="n">t</span><span class="o">.</span><span class="n">value</span> <span class="o">//</span> <span class="n">freq</span><span class="o">.</span><span class="n">delta</span><span class="o">.</span><span class="n">value</span><span class="p">)</span> <span class="o">*</span> <span class="n">freq</span><span class="o">.</span><span class="n">delta</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [244]: </span><span class="n">ts</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="n">partial</span><span class="p">(</span><span class="nb">round</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;3T&apos;</span><span class="p">))</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gr">Out[244]: </span>
<span class="go">2014-01-01     0</span>
<span class="go">2014-01-02     1</span>
<span class="go">2014-01-03     2</span>
<span class="go">2014-01-04     3</span>
<span class="go">2014-01-05     4</span>
<span class="go">2014-01-06     5</span>
<span class="go">2014-01-07     6</span>
<span class="go">              ..</span>
<span class="go">2014-04-04    93</span>
<span class="go">2014-04-05    94</span>
<span class="go">2014-04-06    95</span>
<span class="go">2014-04-07    96</span>
<span class="go">2014-04-08    97</span>
<span class="go">2014-04-09    98</span>
<span class="go">2014-04-10    99</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
</div>
<div class="section" id="aggregation">
<h3><span class="yiyi-st" id="yiyi-612">Aggregation</span></h3>
<p><span class="yiyi-st" id="yiyi-613">类似于<a class="reference internal" href="groupby.html#groupby-aggregate"><span class="std std-ref">groupby aggregates</span></a>和<a class="reference internal" href="computation.html#stats-aggregate"><span class="std std-ref">window functions</span></a>，可以选择性地重新采样<code class="docutils literal"><span class="pre">Resampler</span></code>。</span></p>
<p><span class="yiyi-st" id="yiyi-614">重新取样<code class="docutils literal"><span class="pre">DataFrame</span></code>，默认情况下将对具有相同函数的所有列执行操作。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [245]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span>
<span class="gp">   .....:</span>                   <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/1/2012&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;S&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">1000</span><span class="p">),</span>
<span class="gp">   .....:</span>                   <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [246]: </span><span class="n">r</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;3T&apos;</span><span class="p">)</span>

<span class="gp">In [247]: </span><span class="n">r</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[247]: </span>
<span class="go">                            A         B         C</span>
<span class="go">2012-01-01 00:00:00 -0.220339  0.034854 -0.073757</span>
<span class="go">2012-01-01 00:03:00  0.037070  0.040013  0.053754</span>
<span class="go">2012-01-01 00:06:00 -0.041597 -0.144562 -0.007614</span>
<span class="go">2012-01-01 00:09:00  0.043127 -0.076432 -0.032570</span>
<span class="go">2012-01-01 00:12:00 -0.027609  0.054618  0.056878</span>
<span class="go">2012-01-01 00:15:00 -0.014181  0.043958  0.077734</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-615">我们可以使用标准的getitem选择特定的一列或多列。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [248]: </span><span class="n">r</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[248]: </span>
<span class="go">2012-01-01 00:00:00   -0.220339</span>
<span class="go">2012-01-01 00:03:00    0.037070</span>
<span class="go">2012-01-01 00:06:00   -0.041597</span>
<span class="go">2012-01-01 00:09:00    0.043127</span>
<span class="go">2012-01-01 00:12:00   -0.027609</span>
<span class="go">2012-01-01 00:15:00   -0.014181</span>
<span class="go">Freq: 3T, Name: A, dtype: float64</span>

<span class="gp">In [249]: </span><span class="n">r</span><span class="p">[[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;B&apos;</span><span class="p">]]</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[249]: </span>
<span class="go">                            A         B</span>
<span class="go">2012-01-01 00:00:00 -0.220339  0.034854</span>
<span class="go">2012-01-01 00:03:00  0.037070  0.040013</span>
<span class="go">2012-01-01 00:06:00 -0.041597 -0.144562</span>
<span class="go">2012-01-01 00:09:00  0.043127 -0.076432</span>
<span class="go">2012-01-01 00:12:00 -0.027609  0.054618</span>
<span class="go">2012-01-01 00:15:00 -0.014181  0.043958</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-616">你可以传递一个list或dict的函数做聚合，输出一个DataFrame：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [250]: </span><span class="n">r</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">agg</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">])</span>
<span class="gr">Out[250]: </span>
<span class="go">                           sum      mean       std</span>
<span class="go">2012-01-01 00:00:00 -39.660974 -0.220339  1.033912</span>
<span class="go">2012-01-01 00:03:00   6.672559  0.037070  0.971503</span>
<span class="go">2012-01-01 00:06:00  -7.487453 -0.041597  1.018418</span>
<span class="go">2012-01-01 00:09:00   7.762901  0.043127  1.025842</span>
<span class="go">2012-01-01 00:12:00  -4.969624 -0.027609  0.961649</span>
<span class="go">2012-01-01 00:15:00  -1.418119 -0.014181  0.978847</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-617">如果传递了dict，则键将用于命名列。</span><span class="yiyi-st" id="yiyi-618">否则将使用函数的名称（存储在函数对象中）。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [251]: </span><span class="n">r</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">agg</span><span class="p">({</span><span class="s1">&apos;result1&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">,</span>
<span class="gp">   .....:</span>             <span class="s1">&apos;result2&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">})</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[251]: </span>
<span class="go">                      result2    result1</span>
<span class="go">2012-01-01 00:00:00 -0.220339 -39.660974</span>
<span class="go">2012-01-01 00:03:00  0.037070   6.672559</span>
<span class="go">2012-01-01 00:06:00 -0.041597  -7.487453</span>
<span class="go">2012-01-01 00:09:00  0.043127   7.762901</span>
<span class="go">2012-01-01 00:12:00 -0.027609  -4.969624</span>
<span class="go">2012-01-01 00:15:00 -0.014181  -1.418119</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-619">在重新采样的DataFrame上，可以传递要应用于每个列的函数列表，这会生成具有层次索引的聚合结果：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [252]: </span><span class="n">r</span><span class="o">.</span><span class="n">agg</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">])</span>
<span class="gr">Out[252]: </span>
<span class="go">                             A                    B                    C  \</span>
<span class="go">                           sum      mean        sum      mean        sum   </span>
<span class="go">2012-01-01 00:00:00 -39.660974 -0.220339   6.273786  0.034854 -13.276324   </span>
<span class="go">2012-01-01 00:03:00   6.672559  0.037070   7.202361  0.040013   9.675632   </span>
<span class="go">2012-01-01 00:06:00  -7.487453 -0.041597 -26.021155 -0.144562  -1.370600   </span>
<span class="go">2012-01-01 00:09:00   7.762901  0.043127 -13.757837 -0.076432  -5.862640   </span>
<span class="go">2012-01-01 00:12:00  -4.969624 -0.027609   9.831208  0.054618  10.237970   </span>
<span class="go">2012-01-01 00:15:00  -1.418119 -0.014181   4.395766  0.043958   7.773442   </span>

<span class="go">                               </span>
<span class="go">                         mean  </span>
<span class="go">2012-01-01 00:00:00 -0.073757  </span>
<span class="go">2012-01-01 00:03:00  0.053754  </span>
<span class="go">2012-01-01 00:06:00 -0.007614  </span>
<span class="go">2012-01-01 00:09:00 -0.032570  </span>
<span class="go">2012-01-01 00:12:00  0.056878  </span>
<span class="go">2012-01-01 00:15:00  0.077734  </span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-620">通过将dict传递到<code class="docutils literal"><span class="pre">aggregate</span></code>，您可以对DataFrame的列应用不同的聚合：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [253]: </span><span class="n">r</span><span class="o">.</span><span class="n">agg</span><span class="p">({</span><span class="s1">&apos;A&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">,</span>
<span class="gp">   .....:</span>        <span class="s1">&apos;B&apos;</span> <span class="p">:</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">ddof</span><span class="o">=</span><span class="mi">1</span><span class="p">)})</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[253]: </span>
<span class="go">                             A         B</span>
<span class="go">2012-01-01 00:00:00 -39.660974  1.004756</span>
<span class="go">2012-01-01 00:03:00   6.672559  0.963559</span>
<span class="go">2012-01-01 00:06:00  -7.487453  0.950766</span>
<span class="go">2012-01-01 00:09:00   7.762901  0.949182</span>
<span class="go">2012-01-01 00:12:00  -4.969624  1.093736</span>
<span class="go">2012-01-01 00:15:00  -1.418119  1.028869</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-621">函数名也可以是字符串。</span><span class="yiyi-st" id="yiyi-622">为了使字符串有效，它必须在重采样对象上实现</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [254]: </span><span class="n">r</span><span class="o">.</span><span class="n">agg</span><span class="p">({</span><span class="s1">&apos;A&apos;</span> <span class="p">:</span> <span class="s1">&apos;sum&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span> <span class="p">:</span> <span class="s1">&apos;std&apos;</span><span class="p">})</span>
<span class="gr">Out[254]: </span>
<span class="go">                             A         B</span>
<span class="go">2012-01-01 00:00:00 -39.660974  1.004756</span>
<span class="go">2012-01-01 00:03:00   6.672559  0.963559</span>
<span class="go">2012-01-01 00:06:00  -7.487453  0.950766</span>
<span class="go">2012-01-01 00:09:00   7.762901  0.949182</span>
<span class="go">2012-01-01 00:12:00  -4.969624  1.093736</span>
<span class="go">2012-01-01 00:15:00  -1.418119  1.028869</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-623">此外，您还可以为每个列分别指定多个聚合函数。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [255]: </span><span class="n">r</span><span class="o">.</span><span class="n">agg</span><span class="p">({</span><span class="s1">&apos;A&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="s1">&apos;sum&apos;</span><span class="p">,</span><span class="s1">&apos;std&apos;</span><span class="p">],</span> <span class="s1">&apos;B&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="s1">&apos;mean&apos;</span><span class="p">,</span><span class="s1">&apos;std&apos;</span><span class="p">]</span> <span class="p">})</span>
<span class="gr">Out[255]: </span>
<span class="go">                             A                   B          </span>
<span class="go">                           sum       std      mean       std</span>
<span class="go">2012-01-01 00:00:00 -39.660974  1.033912  0.034854  1.004756</span>
<span class="go">2012-01-01 00:03:00   6.672559  0.971503  0.040013  0.963559</span>
<span class="go">2012-01-01 00:06:00  -7.487453  1.018418 -0.144562  0.950766</span>
<span class="go">2012-01-01 00:09:00   7.762901  1.025842 -0.076432  0.949182</span>
<span class="go">2012-01-01 00:12:00  -4.969624  0.961649  0.054618  1.093736</span>
<span class="go">2012-01-01 00:15:00  -1.418119  0.978847  0.043958  1.028869</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-624">如果<code class="docutils literal"><span class="pre">DataFrame</span></code>没有datetimelike索引，而是要根据框架中的datetimelike列重新取样，则它可以传递到<code class="docutils literal"><span class="pre">on</span></code>关键字。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [256]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;date&apos;</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;2015-01-01&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;W&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;a&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">)},</span>
<span class="gp">   .....:</span>                   <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_arrays</span><span class="p">([</span>
<span class="gp">   .....:</span>                            <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">],</span>
<span class="gp">   .....:</span>                            <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;2015-01-01&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;W&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">)],</span>
<span class="gp">   .....:</span>                        <span class="n">names</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;v&apos;</span><span class="p">,</span><span class="s1">&apos;d&apos;</span><span class="p">]))</span>
<span class="gp">   .....:</span> 

<span class="gp">In [257]: </span><span class="n">df</span>
<span class="gr">Out[257]: </span>
<span class="go">              a       date</span>
<span class="go">v d                       </span>
<span class="go">1 2015-01-04  0 2015-01-04</span>
<span class="go">2 2015-01-11  1 2015-01-11</span>
<span class="go">3 2015-01-18  2 2015-01-18</span>
<span class="go">4 2015-01-25  3 2015-01-25</span>
<span class="go">5 2015-02-01  4 2015-02-01</span>

<span class="gp">In [258]: </span><span class="n">df</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s1">&apos;date&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gr">Out[258]: </span>
<span class="go">            a</span>
<span class="go">date         </span>
<span class="go">2015-01-31  6</span>
<span class="go">2015-02-28  4</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-625">类似地，如果您想要通过<code class="docutils literal"><span class="pre">MultiIndex</span></code>的数据类型级别重新采样，则其名称或位置可以传递到<code class="docutils literal"><span class="pre">level</span></code>关键字。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [259]: </span><span class="n">df</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="s1">&apos;d&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gr">Out[259]: </span>
<span class="go">            a</span>
<span class="go">d            </span>
<span class="go">2015-01-31  6</span>
<span class="go">2015-02-28  4</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="time-span-representation">
<span id="timeseries-periods"></span><h2><span class="yiyi-st" id="yiyi-626">Time Span Representation</span></h2>
<p><span class="yiyi-st" id="yiyi-627">规则的时间间隔由pandas中的<code class="docutils literal"><span class="pre">Period</span></code>对象表示，而<code class="docutils literal"><span class="pre">Period</span></code>对象的序列被收集在<code class="docutils literal"><span class="pre">PeriodIndex</span></code>中，函数<code class="docutils literal"><span class="pre">period_range</span></code>。</span></p>
<div class="section" id="period">
<h3><span class="yiyi-st" id="yiyi-628">Period</span></h3>
<p><span class="yiyi-st" id="yiyi-629">A <code class="docutils literal"><span class="pre">Period</span></code>表示时间跨度（例如，一天，一个月，一个季度等）。</span><span class="yiyi-st" id="yiyi-630">您可以使用下面的频率别名通过<code class="docutils literal"><span class="pre">freq</span></code>关键字指定范围。</span><span class="yiyi-st" id="yiyi-631">因为<code class="docutils literal"><span class="pre">freq</span></code>表示<code class="docutils literal"><span class="pre">Period</span></code>的跨度，所以不能像“-3D”那样为负。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [260]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;A-DEC&apos;</span><span class="p">)</span>
<span class="gr">Out[260]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012&apos;</span><span class="p">,</span> <span class="s1">&apos;A-DEC&apos;</span><span class="p">)</span>

<span class="gp">In [261]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-1-1&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>
<span class="gr">Out[261]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-01-01&apos;</span><span class="p">,</span> <span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [262]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-1-1 19:00&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;H&apos;</span><span class="p">)</span>
<span class="gr">Out[262]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-01-01 19:00&apos;</span><span class="p">,</span> <span class="s1">&apos;H&apos;</span><span class="p">)</span>

<span class="gp">In [263]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-1-1 19:00&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;5H&apos;</span><span class="p">)</span>
<span class="gr">Out[263]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-01-01 19:00&apos;</span><span class="p">,</span> <span class="s1">&apos;5H&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-632">从周期中增加和减去整数将周期移动自己的频率。</span><span class="yiyi-st" id="yiyi-633">不允许在<code class="docutils literal"><span class="pre">Period</span></code>与不同<code class="docutils literal"><span class="pre">freq</span></code>（跨度）之间进行算术。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [264]: </span><span class="n">p</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;A-DEC&apos;</span><span class="p">)</span>

<span class="gp">In [265]: </span><span class="n">p</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gr">Out[265]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2013&apos;</span><span class="p">,</span> <span class="s1">&apos;A-DEC&apos;</span><span class="p">)</span>

<span class="gp">In [266]: </span><span class="n">p</span> <span class="o">-</span> <span class="mi">3</span>
<span class="gr">Out[266]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2009&apos;</span><span class="p">,</span> <span class="s1">&apos;A-DEC&apos;</span><span class="p">)</span>

<span class="gp">In [267]: </span><span class="n">p</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-01&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;2M&apos;</span><span class="p">)</span>

<span class="gp">In [268]: </span><span class="n">p</span> <span class="o">+</span> <span class="mi">2</span>
<span class="gr">Out[268]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-05&apos;</span><span class="p">,</span> <span class="s1">&apos;2M&apos;</span><span class="p">)</span>

<span class="gp">In [269]: </span><span class="n">p</span> <span class="o">-</span> <span class="mi">1</span>
<span class="gr">Out[269]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2011-11&apos;</span><span class="p">,</span> <span class="s1">&apos;2M&apos;</span><span class="p">)</span>

<span class="gp">In [270]: </span><span class="n">p</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-01&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;3M&apos;</span><span class="p">)</span>
<span class="go">---------------------------------------------------------------------------</span>
<span class="go">IncompatibleFrequency                     Traceback (most recent call last)</span>
<span class="go">&lt;ipython-input-270-ff54ce3238f5&gt; in &lt;module&gt;()</span>
<span class="go">----&gt; 1 p == pd.Period(&apos;2012-01&apos;, freq=&apos;3M&apos;)</span>

<span class="go">/home/joris/scipy/pandas/pandas/src/period.pyx in pandas._period._Period.__richcmp__ (pandas/src/period.c:11340)()</span>
<span class="go">    729             if other.freq != self.freq:</span>
<span class="go">    730                 msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr)</span>
<span class="go">--&gt; 731                 raise IncompatibleFrequency(msg)</span>
<span class="go">    732             return PyObject_RichCompareBool(self.ordinal, other.ordinal, op)</span>
<span class="go">    733         elif other is tslib.NaT:</span>

<span class="go">IncompatibleFrequency: Input has different freq=3M from Period(freq=2M)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-634">如果<code class="docutils literal"><span class="pre">Period</span></code> freq是每日或更高（<code class="docutils literal"><span class="pre">D</span></code>，<code class="docutils literal"><span class="pre">H</span></code>，<code class="docutils literal"><span class="pre">T</span></code>，<code class="docutils literal"><span class="pre">S</span></code> ，<code class="docutils literal"><span class="pre">L</span></code>，<code class="docutils literal"><span class="pre">U</span></code>，<code class="docutils literal"><span class="pre">N</span></code>），<code class="docutils literal"><span class="pre">offsets</span></code>和<code class="docutils literal"><span class="pre">timedelta</span></code>在有相同的频率的情况下可以使用，否则将引发ValueError。</span><span class="yiyi-st" id="yiyi-635">否则，将引发<code class="docutils literal"><span class="pre">ValueError</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [271]: </span><span class="n">p</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2014-07-01 09:00&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;H&apos;</span><span class="p">)</span>

<span class="gp">In [272]: </span><span class="n">p</span> <span class="o">+</span> <span class="n">Hour</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[272]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2014-07-01 11:00&apos;</span><span class="p">,</span> <span class="s1">&apos;H&apos;</span><span class="p">)</span>

<span class="gp">In [273]: </span><span class="n">p</span> <span class="o">+</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=</span><span class="mi">120</span><span class="p">)</span>
<span class="gr">Out[273]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2014-07-01 11:00&apos;</span><span class="p">,</span> <span class="s1">&apos;H&apos;</span><span class="p">)</span>

<span class="gp">In [274]: </span><span class="n">p</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="mi">7200</span><span class="p">,</span> <span class="s1">&apos;s&apos;</span><span class="p">)</span>
<span class="gr">Out[274]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2014-07-01 11:00&apos;</span><span class="p">,</span> <span class="s1">&apos;H&apos;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="n">p</span> <span class="o">+</span> <span class="n">Minute</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="go">Traceback</span>
<span class="go">   ...</span>
<span class="go">ValueError: Input has different freq from Period(freq=H)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-636">如果<code class="docutils literal"><span class="pre">Period</span></code>有其他频率，则只能添加相同的<code class="docutils literal"><span class="pre">offsets</span></code>。</span><span class="yiyi-st" id="yiyi-637">否则，将引发<code class="docutils literal"><span class="pre">ValueError</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [275]: </span><span class="n">p</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2014-07&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [276]: </span><span class="n">p</span> <span class="o">+</span> <span class="n">MonthEnd</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gr">Out[276]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2014-10&apos;</span><span class="p">,</span> <span class="s1">&apos;M&apos;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="n">p</span> <span class="o">+</span> <span class="n">MonthBegin</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">Traceback</span>
<span class="go">   ...</span>
<span class="go">ValueError: Input has different freq from Period(freq=M)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-638">以相同频率的<code class="docutils literal"><span class="pre">Period</span></code>实例的差异将返回它们之间的频率单位数：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [277]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;A-DEC&apos;</span><span class="p">)</span> <span class="o">-</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2002&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;A-DEC&apos;</span><span class="p">)</span>
<span class="gr">Out[277]: </span><span class="mi">10</span>
</pre></div>
</div>
</div>
<div class="section" id="periodindex-and-period-range">
<h3><span class="yiyi-st" id="yiyi-639">PeriodIndex and period_range</span></h3>
<p><span class="yiyi-st" id="yiyi-640">可以在<code class="docutils literal"><span class="pre">PeriodIndex</span></code>中收集<code class="docutils literal"><span class="pre">Period</span></code>对象的规则序列，其可以使用<code class="docutils literal"><span class="pre">period_range</span></code>方便函数构建：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [278]: </span><span class="n">prng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">period_range</span><span class="p">(</span><span class="s1">&apos;1/1/2011&apos;</span><span class="p">,</span> <span class="s1">&apos;1/1/2012&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [279]: </span><span class="n">prng</span>
<span class="gr">Out[279]: </span>
<span class="go">PeriodIndex([&apos;2011-01&apos;, &apos;2011-02&apos;, &apos;2011-03&apos;, &apos;2011-04&apos;, &apos;2011-05&apos;, &apos;2011-06&apos;,</span>
<span class="go">             &apos;2011-07&apos;, &apos;2011-08&apos;, &apos;2011-09&apos;, &apos;2011-10&apos;, &apos;2011-11&apos;, &apos;2011-12&apos;,</span>
<span class="go">             &apos;2012-01&apos;],</span>
<span class="go">            dtype=&apos;period[M]&apos;, freq=&apos;M&apos;)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-641"><code class="docutils literal"><span class="pre">PeriodIndex</span></code>构造函数也可以直接使用：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [280]: </span><span class="n">pd</span><span class="o">.</span><span class="n">PeriodIndex</span><span class="p">([</span><span class="s1">&apos;2011-1&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-2&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-3&apos;</span><span class="p">],</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>
<span class="gr">Out[280]: </span><span class="n">PeriodIndex</span><span class="p">([</span><span class="s1">&apos;2011-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-02&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-03&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;period[M]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-642">传递乘法频率输出具有乘法跨度的<code class="docutils literal"><span class="pre">Period</span></code>的序列。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [281]: </span><span class="n">pd</span><span class="o">.</span><span class="n">PeriodIndex</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="s1">&apos;2014-01&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;3M&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="gr">Out[281]: </span><span class="n">PeriodIndex</span><span class="p">([</span><span class="s1">&apos;2014-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2014-04&apos;</span><span class="p">,</span> <span class="s1">&apos;2014-07&apos;</span><span class="p">,</span> <span class="s1">&apos;2014-10&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;period[3M]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;3M&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-643">与<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>一样，<code class="docutils literal"><span class="pre">PeriodIndex</span></code>也可以用于索引pandas对象：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [282]: </span><span class="n">ps</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">prng</span><span class="p">)),</span> <span class="n">prng</span><span class="p">)</span>

<span class="gp">In [283]: </span><span class="n">ps</span>
<span class="gr">Out[283]: </span>
<span class="go">2011-01   -1.022670</span>
<span class="go">2011-02    1.371155</span>
<span class="go">2011-03    1.035277</span>
<span class="go">2011-04    1.694400</span>
<span class="go">2011-05   -1.659733</span>
<span class="go">2011-06    0.511432</span>
<span class="go">2011-07    0.433176</span>
<span class="go">2011-08   -0.317955</span>
<span class="go">2011-09   -0.517114</span>
<span class="go">2011-10   -0.310466</span>
<span class="go">2011-11    0.543957</span>
<span class="go">2011-12    0.492003</span>
<span class="go">2012-01    0.193420</span>
<span class="go">Freq: M, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-644"><code class="docutils literal"><span class="pre">PeriodIndex</span></code>支持与<code class="docutils literal"><span class="pre">Period</span></code>相同的规则进行加法和减法。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [284]: </span><span class="n">idx</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">period_range</span><span class="p">(</span><span class="s1">&apos;2014-07-01 09:00&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;H&apos;</span><span class="p">)</span>

<span class="gp">In [285]: </span><span class="n">idx</span>
<span class="gr">Out[285]: </span>
<span class="go">PeriodIndex([&apos;2014-07-01 09:00&apos;, &apos;2014-07-01 10:00&apos;, &apos;2014-07-01 11:00&apos;,</span>
<span class="go">             &apos;2014-07-01 12:00&apos;, &apos;2014-07-01 13:00&apos;],</span>
<span class="go">            dtype=&apos;period[H]&apos;, freq=&apos;H&apos;)</span>

<span class="gp">In [286]: </span><span class="n">idx</span> <span class="o">+</span> <span class="n">Hour</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[286]: </span>
<span class="go">PeriodIndex([&apos;2014-07-01 11:00&apos;, &apos;2014-07-01 12:00&apos;, &apos;2014-07-01 13:00&apos;,</span>
<span class="go">             &apos;2014-07-01 14:00&apos;, &apos;2014-07-01 15:00&apos;],</span>
<span class="go">            dtype=&apos;period[H]&apos;, freq=&apos;H&apos;)</span>

<span class="gp">In [287]: </span><span class="n">idx</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">period_range</span><span class="p">(</span><span class="s1">&apos;2014-07&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [288]: </span><span class="n">idx</span>
<span class="gr">Out[288]: </span><span class="n">PeriodIndex</span><span class="p">([</span><span class="s1">&apos;2014-07&apos;</span><span class="p">,</span> <span class="s1">&apos;2014-08&apos;</span><span class="p">,</span> <span class="s1">&apos;2014-09&apos;</span><span class="p">,</span> <span class="s1">&apos;2014-10&apos;</span><span class="p">,</span> <span class="s1">&apos;2014-11&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;period[M]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [289]: </span><span class="n">idx</span> <span class="o">+</span> <span class="n">MonthEnd</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gr">Out[289]: </span><span class="n">PeriodIndex</span><span class="p">([</span><span class="s1">&apos;2014-10&apos;</span><span class="p">,</span> <span class="s1">&apos;2014-11&apos;</span><span class="p">,</span> <span class="s1">&apos;2014-12&apos;</span><span class="p">,</span> <span class="s1">&apos;2015-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2015-02&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;period[M]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-645"><code class="docutils literal"><span class="pre">PeriodIndex</span></code>有自己的dtype <code class="docutils literal"><span class="pre">period</span></code>，请参阅<a class="reference internal" href="#timeseries-period-dtype"><span class="std std-ref">Period Dtypes</span></a>。</span></p>
</div>
<div class="section" id="period-dtypes">
<span id="timeseries-period-dtype"></span><h3><span class="yiyi-st" id="yiyi-646">Period Dtypes</span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-647"><span class="versionmodified">版本0.19.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-648"><code class="docutils literal"><span class="pre">PeriodIndex</span></code>具有自定义<code class="docutils literal"><span class="pre">period</span></code> dtype。</span><span class="yiyi-st" id="yiyi-649">这是类似于<a class="reference internal" href="#timeseries-timezone-series"><span class="std std-ref">timezone aware dtype</span></a>（<code class="docutils literal"><span class="pre">datetime64 [ns，</span> <span class="pre">tz]</span></code>）的pandas扩展dtype。</span></p>
<p><span class="yiyi-st" id="yiyi-650">The <code class="docutils literal"><span class="pre">period</span></code> dtype holds the <code class="docutils literal"><span class="pre">freq</span></code> attribute and is represented with <code class="docutils literal"><span class="pre">period[freq]</span></code> like <code class="docutils literal"><span class="pre">period[D]</span></code> or <code class="docutils literal"><span class="pre">period[M]</span></code>, using <a class="reference internal" href="#timeseries-offset-aliases"><span class="std std-ref">frequency strings</span></a>.</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [290]: </span><span class="n">pi</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">period_range</span><span class="p">(</span><span class="s1">&apos;2016-01-01&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [291]: </span><span class="n">pi</span>
<span class="gr">Out[291]: </span><span class="n">PeriodIndex</span><span class="p">([</span><span class="s1">&apos;2016-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2016-02&apos;</span><span class="p">,</span> <span class="s1">&apos;2016-03&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;period[M]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [292]: </span><span class="n">pi</span><span class="o">.</span><span class="n">dtype</span>
<span class="gr">Out[292]: </span><span class="n">period</span><span class="p">[</span><span class="n">M</span><span class="p">]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-651"><code class="docutils literal"><span class="pre">period</span></code> dtype可以在<code class="docutils literal"><span class="pre">.astype(...)</span></code>中使用。</span><span class="yiyi-st" id="yiyi-652">它允许像<code class="docutils literal"><span class="pre">.asfreq()</span></code>一样更改<code class="docutils literal"><span class="pre">PeriodIndex</span></code>的<code class="docutils literal"><span class="pre">freq</span></code>，并将<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>转换为<code class="docutils literal"><span class="pre">PeriodIndex</span></code> like <code class="docutils literal"><span class="pre">to_period()</span></code>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># change monthly freq to daily freq</span>
<span class="gp">In [293]: </span><span class="n">pi</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;period[D]&apos;</span><span class="p">)</span>
<span class="gr">Out[293]: </span><span class="n">PeriodIndex</span><span class="p">([</span><span class="s1">&apos;2016-01-31&apos;</span><span class="p">,</span> <span class="s1">&apos;2016-02-29&apos;</span><span class="p">,</span> <span class="s1">&apos;2016-03-31&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;period[D]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="c"># convert to DatetimeIndex</span>
<span class="gp">In [294]: </span><span class="n">pi</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">)</span>
<span class="gr">Out[294]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2016-01-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2016-02-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2016-03-01&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;MS&apos;</span><span class="p">)</span>

<span class="c"># convert to PeriodIndex</span>
<span class="gp">In [295]: </span><span class="n">dti</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;2011-01-01&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>

<span class="gp">In [296]: </span><span class="n">dti</span>
<span class="gr">Out[296]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2011-01-31&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-02-28&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-03-31&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [297]: </span><span class="n">dti</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;period[M]&apos;</span><span class="p">)</span>
<span class="gr">Out[297]: </span><span class="n">PeriodIndex</span><span class="p">([</span><span class="s1">&apos;2011-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-02&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-03&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;period[M]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="periodindex-partial-string-indexing">
<h3><span class="yiyi-st" id="yiyi-653">PeriodIndex Partial String Indexing</span></h3>
<p><span class="yiyi-st" id="yiyi-654">您可以使用<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>相同的方式将日期和字符串传递到<code class="docutils literal"><span class="pre">Series</span></code>和<code class="docutils literal"><span class="pre">DataFrame</span></code>和<code class="docutils literal"><span class="pre">PeriodIndex</span></code>。</span><span class="yiyi-st" id="yiyi-655">有关详细信息，请参阅<a class="reference internal" href="#timeseries-partialindexing"><span class="std std-ref">DatetimeIndex Partial String Indexing</span></a>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [298]: </span><span class="n">ps</span><span class="p">[</span><span class="s1">&apos;2011-01&apos;</span><span class="p">]</span>
<span class="gr">Out[298]: </span><span class="o">-</span><span class="mf">1.022669594890105</span>

<span class="gp">In [299]: </span><span class="n">ps</span><span class="p">[</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2011</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">25</span><span class="p">):]</span>
<span class="gr">Out[299]: </span>
<span class="go">2011-12    0.492003</span>
<span class="go">2012-01    0.193420</span>
<span class="go">Freq: M, dtype: float64</span>

<span class="gp">In [300]: </span><span class="n">ps</span><span class="p">[</span><span class="s1">&apos;10/31/2011&apos;</span><span class="p">:</span><span class="s1">&apos;12/31/2011&apos;</span><span class="p">]</span>
<span class="gr">Out[300]: </span>
<span class="go">2011-10   -0.310466</span>
<span class="go">2011-11    0.543957</span>
<span class="go">2011-12    0.492003</span>
<span class="go">Freq: M, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-656">传递表示比<code class="docutils literal"><span class="pre">PeriodIndex</span></code>更低的频率的字符串返回部分分片数据。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [301]: </span><span class="n">ps</span><span class="p">[</span><span class="s1">&apos;2011&apos;</span><span class="p">]</span>
<span class="gr">Out[301]: </span>
<span class="go">2011-01   -1.022670</span>
<span class="go">2011-02    1.371155</span>
<span class="go">2011-03    1.035277</span>
<span class="go">2011-04    1.694400</span>
<span class="go">2011-05   -1.659733</span>
<span class="go">2011-06    0.511432</span>
<span class="go">2011-07    0.433176</span>
<span class="go">2011-08   -0.317955</span>
<span class="go">2011-09   -0.517114</span>
<span class="go">2011-10   -0.310466</span>
<span class="go">2011-11    0.543957</span>
<span class="go">2011-12    0.492003</span>
<span class="go">Freq: M, dtype: float64</span>

<span class="gp">In [302]: </span><span class="n">dfp</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">600</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span>
<span class="gp">   .....:</span>                    <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                    <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">period_range</span><span class="p">(</span><span class="s1">&apos;2013-01-01 9:00&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">600</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;T&apos;</span><span class="p">))</span>
<span class="gp">   .....:</span> 

<span class="gp">In [303]: </span><span class="n">dfp</span>
<span class="gr">Out[303]: </span>
<span class="go">                         A</span>
<span class="go">2013-01-01 09:00  0.197720</span>
<span class="go">2013-01-01 09:01 -0.284769</span>
<span class="go">2013-01-01 09:02  0.061491</span>
<span class="go">2013-01-01 09:03  1.630257</span>
<span class="go">2013-01-01 09:04  2.042442</span>
<span class="go">2013-01-01 09:05 -0.804392</span>
<span class="go">2013-01-01 09:06  0.212760</span>
<span class="go">...                    ...</span>
<span class="go">2013-01-01 18:53  0.150586</span>
<span class="go">2013-01-01 18:54 -0.679569</span>
<span class="go">2013-01-01 18:55 -0.910216</span>
<span class="go">2013-01-01 18:56 -0.413168</span>
<span class="go">2013-01-01 18:57 -0.247752</span>
<span class="go">2013-01-01 18:58  1.590875</span>
<span class="go">2013-01-01 18:59 -2.005294</span>

<span class="go">[600 rows x 1 columns]</span>

<span class="gp">In [304]: </span><span class="n">dfp</span><span class="p">[</span><span class="s1">&apos;2013-01-01 10H&apos;</span><span class="p">]</span>
<span class="gr">Out[304]: </span>
<span class="go">                         A</span>
<span class="go">2013-01-01 10:00 -0.569936</span>
<span class="go">2013-01-01 10:01 -1.179183</span>
<span class="go">2013-01-01 10:02 -0.838602</span>
<span class="go">2013-01-01 10:03 -1.727539</span>
<span class="go">2013-01-01 10:04  1.334027</span>
<span class="go">2013-01-01 10:05  0.417423</span>
<span class="go">2013-01-01 10:06 -0.221189</span>
<span class="go">...                    ...</span>
<span class="go">2013-01-01 10:53 -0.375925</span>
<span class="go">2013-01-01 10:54  0.212750</span>
<span class="go">2013-01-01 10:55 -0.592417</span>
<span class="go">2013-01-01 10:56 -0.466064</span>
<span class="go">2013-01-01 10:57 -1.715347</span>
<span class="go">2013-01-01 10:58 -0.634913</span>
<span class="go">2013-01-01 10:59 -0.809471</span>

<span class="go">[60 rows x 1 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-657">与<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>一样，端点将包含在结果中。</span><span class="yiyi-st" id="yiyi-658">下面的示例将数据从10:00开始到11:59。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [305]: </span><span class="n">dfp</span><span class="p">[</span><span class="s1">&apos;2013-01-01 10H&apos;</span><span class="p">:</span><span class="s1">&apos;2013-01-01 11H&apos;</span><span class="p">]</span>
<span class="gr">Out[305]: </span>
<span class="go">                         A</span>
<span class="go">2013-01-01 10:00 -0.569936</span>
<span class="go">2013-01-01 10:01 -1.179183</span>
<span class="go">2013-01-01 10:02 -0.838602</span>
<span class="go">2013-01-01 10:03 -1.727539</span>
<span class="go">2013-01-01 10:04  1.334027</span>
<span class="go">2013-01-01 10:05  0.417423</span>
<span class="go">2013-01-01 10:06 -0.221189</span>
<span class="go">...                    ...</span>
<span class="go">2013-01-01 11:53  0.616198</span>
<span class="go">2013-01-01 11:54  2.843156</span>
<span class="go">2013-01-01 11:55  0.572537</span>
<span class="go">2013-01-01 11:56  1.709706</span>
<span class="go">2013-01-01 11:57 -0.205490</span>
<span class="go">2013-01-01 11:58  1.759719</span>
<span class="go">2013-01-01 11:59 -1.181485</span>

<span class="go">[120 rows x 1 columns]</span>
</pre></div>
</div>
</div>
<div class="section" id="frequency-conversion-and-resampling-with-periodindex">
<h3><span class="yiyi-st" id="yiyi-659">Frequency Conversion and Resampling with PeriodIndex</span></h3>
<p><span class="yiyi-st" id="yiyi-660">可以通过<code class="docutils literal"><span class="pre">asfreq</span></code>方法转换<code class="docutils literal"><span class="pre">Period</span></code>和<code class="docutils literal"><span class="pre">PeriodIndex</span></code>的频率。</span><span class="yiyi-st" id="yiyi-661">让我们从2011年财政年度开始，到12月为止：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [306]: </span><span class="n">p</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2011&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;A-DEC&apos;</span><span class="p">)</span>

<span class="gp">In [307]: </span><span class="n">p</span>
<span class="gr">Out[307]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2011&apos;</span><span class="p">,</span> <span class="s1">&apos;A-DEC&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-662">我们可以将其转换为每月频次。</span><span class="yiyi-st" id="yiyi-663">使用<code class="docutils literal"><span class="pre">how</span></code>参数，我们可以指定是返回开始月份还是结束月份：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [308]: </span><span class="n">p</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s1">&apos;start&apos;</span><span class="p">)</span>
<span class="gr">Out[308]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2011-01&apos;</span><span class="p">,</span> <span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [309]: </span><span class="n">p</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s1">&apos;end&apos;</span><span class="p">)</span>
<span class="gr">Out[309]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2011-12&apos;</span><span class="p">,</span> <span class="s1">&apos;M&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-664">为方便起见，提供了shorthands&apos;s&apos;和&apos;e&apos;：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [310]: </span><span class="n">p</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="s1">&apos;s&apos;</span><span class="p">)</span>
<span class="gr">Out[310]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2011-01&apos;</span><span class="p">,</span> <span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [311]: </span><span class="n">p</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="s1">&apos;e&apos;</span><span class="p">)</span>
<span class="gr">Out[311]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2011-12&apos;</span><span class="p">,</span> <span class="s1">&apos;M&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-665">转换为“超周期”（例如，年频率是季度频率的超周期）自动返回包括输入周期的超周期：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [312]: </span><span class="n">p</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2011-12&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [313]: </span><span class="n">p</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="s1">&apos;A-NOV&apos;</span><span class="p">)</span>
<span class="gr">Out[313]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012&apos;</span><span class="p">,</span> <span class="s1">&apos;A-NOV&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-666">注意，由于我们转换为年度结束于11月的年度频率，2011年12月的月度实际上是在2012年A-NOV期间。</span></p>
<p id="timeseries-quarterly"><span class="yiyi-st" id="yiyi-667">具有锚定频率的周期转换对于处理经济，商业和其他领域常见的各种季度数据特别有用。</span><span class="yiyi-st" id="yiyi-668">许多组织定义了相对于其财政年度开始和结束的月份的季度。</span><span class="yiyi-st" id="yiyi-669">因此，2011年第一季度可以在2010年或2011年开始几个月。</span><span class="yiyi-st" id="yiyi-670">通过锚定频率，pandas对于所有季度频率<code class="docutils literal"><span class="pre">Q-JAN</span></code>至<code class="docutils literal"><span class="pre">Q-DEC</span></code>起作用。</span></p>
<p><span class="yiyi-st" id="yiyi-671"><code class="docutils literal"><span class="pre">Q-DEC</span></code>定义常规日历季度：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [314]: </span><span class="n">p</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012Q1&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;Q-DEC&apos;</span><span class="p">)</span>

<span class="gp">In [315]: </span><span class="n">p</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="s1">&apos;D&apos;</span><span class="p">,</span> <span class="s1">&apos;s&apos;</span><span class="p">)</span>
<span class="gr">Out[315]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-01-01&apos;</span><span class="p">,</span> <span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [316]: </span><span class="n">p</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="s1">&apos;D&apos;</span><span class="p">,</span> <span class="s1">&apos;e&apos;</span><span class="p">)</span>
<span class="gr">Out[316]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2012-03-31&apos;</span><span class="p">,</span> <span class="s1">&apos;D&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-672"><code class="docutils literal"><span class="pre">Q-MAR</span></code>定义3月份会计年度结束：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [317]: </span><span class="n">p</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2011Q4&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;Q-MAR&apos;</span><span class="p">)</span>

<span class="gp">In [318]: </span><span class="n">p</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="s1">&apos;D&apos;</span><span class="p">,</span> <span class="s1">&apos;s&apos;</span><span class="p">)</span>
<span class="gr">Out[318]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2011-01-01&apos;</span><span class="p">,</span> <span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [319]: </span><span class="n">p</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="s1">&apos;D&apos;</span><span class="p">,</span> <span class="s1">&apos;e&apos;</span><span class="p">)</span>
<span class="gr">Out[319]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;2011-03-31&apos;</span><span class="p">,</span> <span class="s1">&apos;D&apos;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="converting-between-representations">
<span id="timeseries-interchange"></span><h2><span class="yiyi-st" id="yiyi-673">Converting between Representations</span></h2>
<p><span class="yiyi-st" id="yiyi-674">可以使用<code class="docutils literal"><span class="pre">to_period</span></code>将时间戳记数据转换为PeriodIndexed数据，反之亦然，使用<code class="docutils literal"><span class="pre">to_timestamp</span></code>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [320]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/1/2012&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span>

<span class="gp">In [321]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">rng</span><span class="p">)),</span> <span class="n">index</span><span class="o">=</span><span class="n">rng</span><span class="p">)</span>

<span class="gp">In [322]: </span><span class="n">ts</span>
<span class="gr">Out[322]: </span>
<span class="go">2012-01-31    2.167674</span>
<span class="go">2012-02-29   -1.505130</span>
<span class="go">2012-03-31    1.005802</span>
<span class="go">2012-04-30    0.481525</span>
<span class="go">2012-05-31   -0.352151</span>
<span class="go">Freq: M, dtype: float64</span>

<span class="gp">In [323]: </span><span class="n">ps</span> <span class="o">=</span> <span class="n">ts</span><span class="o">.</span><span class="n">to_period</span><span class="p">()</span>

<span class="gp">In [324]: </span><span class="n">ps</span>
<span class="gr">Out[324]: </span>
<span class="go">2012-01    2.167674</span>
<span class="go">2012-02   -1.505130</span>
<span class="go">2012-03    1.005802</span>
<span class="go">2012-04    0.481525</span>
<span class="go">2012-05   -0.352151</span>
<span class="go">Freq: M, dtype: float64</span>

<span class="gp">In [325]: </span><span class="n">ps</span><span class="o">.</span><span class="n">to_timestamp</span><span class="p">()</span>
<span class="gr">Out[325]: </span>
<span class="go">2012-01-01    2.167674</span>
<span class="go">2012-02-01   -1.505130</span>
<span class="go">2012-03-01    1.005802</span>
<span class="go">2012-04-01    0.481525</span>
<span class="go">2012-05-01   -0.352151</span>
<span class="go">Freq: MS, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-675">请记住，&apos;s&apos;和&apos;e&apos;可用于在时间段的开始或结束处返回时间戳记：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [326]: </span><span class="n">ps</span><span class="o">.</span><span class="n">to_timestamp</span><span class="p">(</span><span class="s1">&apos;D&apos;</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s1">&apos;s&apos;</span><span class="p">)</span>
<span class="gr">Out[326]: </span>
<span class="go">2012-01-01    2.167674</span>
<span class="go">2012-02-01   -1.505130</span>
<span class="go">2012-03-01    1.005802</span>
<span class="go">2012-04-01    0.481525</span>
<span class="go">2012-05-01   -0.352151</span>
<span class="go">Freq: MS, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-676">周期和时间戳之间的转换使一些方便的算术功能可以使用。</span><span class="yiyi-st" id="yiyi-677">在下面的示例中，我们将季度频率转换为以11月结束的年度到季度结束之后的月底的9am：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [327]: </span><span class="n">prng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">period_range</span><span class="p">(</span><span class="s1">&apos;1990Q1&apos;</span><span class="p">,</span> <span class="s1">&apos;2000Q4&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;Q-NOV&apos;</span><span class="p">)</span>

<span class="gp">In [328]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">prng</span><span class="p">)),</span> <span class="n">prng</span><span class="p">)</span>

<span class="gp">In [329]: </span><span class="n">ts</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="p">(</span><span class="n">prng</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="s1">&apos;e&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="s1">&apos;H&apos;</span><span class="p">,</span> <span class="s1">&apos;s&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="mi">9</span>

<span class="gp">In [330]: </span><span class="n">ts</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="gr">Out[330]: </span>
<span class="go">1990-03-01 09:00   -0.608988</span>
<span class="go">1990-06-01 09:00    0.412294</span>
<span class="go">1990-09-01 09:00   -0.715938</span>
<span class="go">1990-12-01 09:00    1.297773</span>
<span class="go">1991-03-01 09:00   -2.260765</span>
<span class="go">Freq: H, dtype: float64</span>
</pre></div>
</div>
</div>
<div class="section" id="representing-out-of-bounds-spans">
<span id="timeseries-oob"></span><h2><span class="yiyi-st" id="yiyi-678">Representing out-of-bounds spans</span></h2>
<p><span class="yiyi-st" id="yiyi-679">If you have data that is outside of the <code class="docutils literal"><span class="pre">Timestamp</span></code> bounds, see <a class="reference internal" href="#timeseries-timestamp-limits"><span class="std std-ref">Timestamp limitations</span></a>, then you can use a <code class="docutils literal"><span class="pre">PeriodIndex</span></code> and/or <code class="docutils literal"><span class="pre">Series</span></code> of <code class="docutils literal"><span class="pre">Periods</span></code> to do computations.</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [331]: </span><span class="n">span</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">period_range</span><span class="p">(</span><span class="s1">&apos;1215-01-01&apos;</span><span class="p">,</span> <span class="s1">&apos;1381-01-01&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [332]: </span><span class="n">span</span>
<span class="gr">Out[332]: </span>
<span class="go">PeriodIndex([&apos;1215-01-01&apos;, &apos;1215-01-02&apos;, &apos;1215-01-03&apos;, &apos;1215-01-04&apos;,</span>
<span class="go">             &apos;1215-01-05&apos;, &apos;1215-01-06&apos;, &apos;1215-01-07&apos;, &apos;1215-01-08&apos;,</span>
<span class="go">             &apos;1215-01-09&apos;, &apos;1215-01-10&apos;,</span>
<span class="go">             ...</span>
<span class="go">             &apos;1380-12-23&apos;, &apos;1380-12-24&apos;, &apos;1380-12-25&apos;, &apos;1380-12-26&apos;,</span>
<span class="go">             &apos;1380-12-27&apos;, &apos;1380-12-28&apos;, &apos;1380-12-29&apos;, &apos;1380-12-30&apos;,</span>
<span class="go">             &apos;1380-12-31&apos;, &apos;1381-01-01&apos;],</span>
<span class="go">            dtype=&apos;period[D]&apos;, length=60632, freq=&apos;D&apos;)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-680">从基于<code class="docutils literal"><span class="pre">int64</span></code>的YYYYMMDD表示形式进行转换。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [333]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">20121231</span><span class="p">,</span> <span class="mi">20141130</span><span class="p">,</span> <span class="mi">99991231</span><span class="p">])</span>

<span class="gp">In [334]: </span><span class="n">s</span>
<span class="gr">Out[334]: </span>
<span class="go">0    20121231</span>
<span class="go">1    20141130</span>
<span class="go">2    99991231</span>
<span class="go">dtype: int64</span>

<span class="gp">In [335]: </span><span class="k">def</span> <span class="nf">conv</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">   .....:</span>     <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="n">year</span> <span class="o">=</span> <span class="n">x</span> <span class="o">//</span> <span class="mi">10000</span><span class="p">,</span> <span class="n">month</span> <span class="o">=</span> <span class="n">x</span><span class="o">//</span><span class="mi">100</span> <span class="o">%</span> <span class="mi">100</span><span class="p">,</span> <span class="n">day</span> <span class="o">=</span> <span class="n">x</span><span class="o">%</span><span class="mi">100</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [336]: </span><span class="n">s</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">conv</span><span class="p">)</span>
<span class="gr">Out[336]: </span>
<span class="go">0   2012-12-31</span>
<span class="go">1   2014-11-30</span>
<span class="go">2   9999-12-31</span>
<span class="go">dtype: object</span>

<span class="gp">In [337]: </span><span class="n">s</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">conv</span><span class="p">)[</span><span class="mi">2</span><span class="p">]</span>
<span class="gr">Out[337]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">&apos;9999-12-31&apos;</span><span class="p">,</span> <span class="s1">&apos;D&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-681">这些可以轻松地转换为<code class="docutils literal"><span class="pre">PeriodIndex</span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [338]: </span><span class="n">span</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">PeriodIndex</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">conv</span><span class="p">))</span>

<span class="gp">In [339]: </span><span class="n">span</span>
<span class="gr">Out[339]: </span><span class="n">PeriodIndex</span><span class="p">([</span><span class="s1">&apos;2012-12-31&apos;</span><span class="p">,</span> <span class="s1">&apos;2014-11-30&apos;</span><span class="p">,</span> <span class="s1">&apos;9999-12-31&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;period[D]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="time-zone-handling">
<span id="timeseries-timezone"></span><h2><span class="yiyi-st" id="yiyi-682">Time Zone Handling</span></h2>
<p><span class="yiyi-st" id="yiyi-683">Pandas为使用<code class="docutils literal"><span class="pre">pytz</span></code>和<code class="docutils literal"><span class="pre">dateutil</span></code>库在不同时区中处理时间戳提供了丰富的支持。</span><span class="yiyi-st" id="yiyi-684"><code class="docutils literal"><span class="pre">dateutil</span></code>支持在0.14.1中是新的，目前仅支持固定偏移和tzfile区域。</span><span class="yiyi-st" id="yiyi-685">默认库为<code class="docutils literal"><span class="pre">pytz</span></code>。</span><span class="yiyi-st" id="yiyi-686">支持<code class="docutils literal"><span class="pre">dateutil</span></code>是为了与其他应用程序兼容，例如如果你在其他python包中使用<code class="docutils literal"><span class="pre">dateutil</span></code>。</span></p>
<div class="section" id="working-with-time-zones">
<h3><span class="yiyi-st" id="yiyi-687">Working with Time Zones</span></h3>
<p><span class="yiyi-st" id="yiyi-688">默认情况下，pandas对象是时区不感知：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [340]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;3/6/2012 00:00&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">15</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [341]: </span><span class="n">rng</span><span class="o">.</span><span class="n">tz</span> <span class="ow">is</span> <span class="bp">None</span>
<span class="gr">Out[341]: </span><span class="bp">True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-689">要提供时区，可以使用<code class="docutils literal"><span class="pre">tz</span></code>关键字<code class="docutils literal"><span class="pre">date_range</span></code>和其他功能。</span><span class="yiyi-st" id="yiyi-690">Dateutil时区字符串与<code class="docutils literal"><span class="pre">pytz</span></code>时区不同，从<code class="docutils literal"><span class="pre">dateutil/</span></code>开始。</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-691">在<code class="docutils literal"><span class="pre">pytz</span></code>中，您可以使用<code class="docutils literal"><span class="pre">从</span> <span class="pre">pytz</span> <span class="pre">import</span>查找常用&gt; <span class="pre">common_timezones，</span> <span class="pre">all_timezones</span></code>。</span></li>
<li><span class="yiyi-st" id="yiyi-692"><code class="docutils literal"><span class="pre">dateutil</span></code>使用操作系统时区，因此没有可用的固定列表。</span><span class="yiyi-st" id="yiyi-693">对于公共区域，名称与<code class="docutils literal"><span class="pre">pytz</span></code>相同。</span></li>
</ul>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># pytz</span>
<span class="gp">In [342]: </span><span class="n">rng_pytz</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;3/6/2012 00:00&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                          <span class="n">tz</span><span class="o">=</span><span class="s1">&apos;Europe/London&apos;</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [343]: </span><span class="n">rng_pytz</span><span class="o">.</span><span class="n">tz</span>
<span class="gr">Out[343]: </span><span class="o">&lt;</span><span class="n">DstTzInfo</span> <span class="s1">&apos;Europe/London&apos;</span> <span class="n">LMT</span><span class="o">-</span><span class="mi">1</span> <span class="n">day</span><span class="p">,</span> <span class="mi">23</span><span class="p">:</span><span class="mi">59</span><span class="p">:</span><span class="mo">00</span> <span class="n">STD</span><span class="o">&gt;</span>

<span class="c"># dateutil</span>
<span class="gp">In [344]: </span><span class="n">rng_dateutil</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;3/6/2012 00:00&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                              <span class="n">tz</span><span class="o">=</span><span class="s1">&apos;dateutil/Europe/London&apos;</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [345]: </span><span class="n">rng_dateutil</span><span class="o">.</span><span class="n">tz</span>
<span class="gr">Out[345]: </span><span class="n">tzfile</span><span class="p">(</span><span class="s1">&apos;/usr/share/zoneinfo/Europe/London&apos;</span><span class="p">)</span>

<span class="c"># dateutil - utc special case</span>
<span class="gp">In [346]: </span><span class="n">rng_utc</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;3/6/2012 00:00&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                         <span class="n">tz</span><span class="o">=</span><span class="n">dateutil</span><span class="o">.</span><span class="n">tz</span><span class="o">.</span><span class="n">tzutc</span><span class="p">())</span>
<span class="gp">   .....:</span> 

<span class="gp">In [347]: </span><span class="n">rng_utc</span><span class="o">.</span><span class="n">tz</span>
<span class="gr">Out[347]: </span><span class="n">tzutc</span><span class="p">()</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-694">请注意，<code class="docutils literal"><span class="pre">UTC</span></code>时区是<code class="docutils literal"><span class="pre">dateutil</span></code>中的特殊情况，应显式构造为<code class="docutils literal"><span class="pre">dateutil.tz.tzutc</span></code>的实例。</span><span class="yiyi-st" id="yiyi-695">您还可以首先显式地构建其他时区，这可以让您更好地控制使用哪个时区：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># pytz</span>
<span class="gp">In [348]: </span><span class="n">tz_pytz</span> <span class="o">=</span> <span class="n">pytz</span><span class="o">.</span><span class="n">timezone</span><span class="p">(</span><span class="s1">&apos;Europe/London&apos;</span><span class="p">)</span>

<span class="gp">In [349]: </span><span class="n">rng_pytz</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;3/6/2012 00:00&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                          <span class="n">tz</span><span class="o">=</span><span class="n">tz_pytz</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [350]: </span><span class="n">rng_pytz</span><span class="o">.</span><span class="n">tz</span> <span class="o">==</span> <span class="n">tz_pytz</span>
<span class="gr">Out[350]: </span><span class="bp">True</span>

<span class="c"># dateutil</span>
<span class="gp">In [351]: </span><span class="n">tz_dateutil</span> <span class="o">=</span> <span class="n">dateutil</span><span class="o">.</span><span class="n">tz</span><span class="o">.</span><span class="n">gettz</span><span class="p">(</span><span class="s1">&apos;Europe/London&apos;</span><span class="p">)</span>

<span class="gp">In [352]: </span><span class="n">rng_dateutil</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;3/6/2012 00:00&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                              <span class="n">tz</span><span class="o">=</span><span class="n">tz_dateutil</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [353]: </span><span class="n">rng_dateutil</span><span class="o">.</span><span class="n">tz</span> <span class="o">==</span> <span class="n">tz_dateutil</span>
<span class="gr">Out[353]: </span><span class="bp">True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-696">时间戳，像Python的<code class="docutils literal"><span class="pre">datetime.datetime</span></code>对象可以是时区naive或时区感知。</span><span class="yiyi-st" id="yiyi-697">原始时间系列和DatetimeIndex对象可以使用<code class="docutils literal"><span class="pre">tz_localize</span></code> <em>localized</em>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [354]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">rng</span><span class="p">)),</span> <span class="n">rng</span><span class="p">)</span>

<span class="gp">In [355]: </span><span class="n">ts_utc</span> <span class="o">=</span> <span class="n">ts</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="s1">&apos;UTC&apos;</span><span class="p">)</span>

<span class="gp">In [356]: </span><span class="n">ts_utc</span>
<span class="gr">Out[356]: </span>
<span class="go">2012-03-06 00:00:00+00:00    0.679135</span>
<span class="go">2012-03-07 00:00:00+00:00    0.345668</span>
<span class="go">2012-03-08 00:00:00+00:00   -1.143903</span>
<span class="go">2012-03-09 00:00:00+00:00    0.487087</span>
<span class="go">2012-03-10 00:00:00+00:00   -1.421073</span>
<span class="go">2012-03-11 00:00:00+00:00   -0.327463</span>
<span class="go">2012-03-12 00:00:00+00:00    0.169899</span>
<span class="go">2012-03-13 00:00:00+00:00    0.867568</span>
<span class="go">2012-03-14 00:00:00+00:00   -0.834122</span>
<span class="go">2012-03-15 00:00:00+00:00   -1.698494</span>
<span class="go">2012-03-16 00:00:00+00:00    0.974717</span>
<span class="go">2012-03-17 00:00:00+00:00    0.966771</span>
<span class="go">2012-03-18 00:00:00+00:00   -0.754168</span>
<span class="go">2012-03-19 00:00:00+00:00   -1.434246</span>
<span class="go">2012-03-20 00:00:00+00:00    0.848935</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-698">同样，您可以首先显式构造时区对象。</span><span class="yiyi-st" id="yiyi-699">您可以使用<code class="docutils literal"><span class="pre">tz_convert</span></code>方法转换pandas对象将tz感知数据转换为另一个时区：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [357]: </span><span class="n">ts_utc</span><span class="o">.</span><span class="n">tz_convert</span><span class="p">(</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">)</span>
<span class="gr">Out[357]: </span>
<span class="go">2012-03-05 19:00:00-05:00    0.679135</span>
<span class="go">2012-03-06 19:00:00-05:00    0.345668</span>
<span class="go">2012-03-07 19:00:00-05:00   -1.143903</span>
<span class="go">2012-03-08 19:00:00-05:00    0.487087</span>
<span class="go">2012-03-09 19:00:00-05:00   -1.421073</span>
<span class="go">2012-03-10 19:00:00-05:00   -0.327463</span>
<span class="go">2012-03-11 20:00:00-04:00    0.169899</span>
<span class="go">2012-03-12 20:00:00-04:00    0.867568</span>
<span class="go">2012-03-13 20:00:00-04:00   -0.834122</span>
<span class="go">2012-03-14 20:00:00-04:00   -1.698494</span>
<span class="go">2012-03-15 20:00:00-04:00    0.974717</span>
<span class="go">2012-03-16 20:00:00-04:00    0.966771</span>
<span class="go">2012-03-17 20:00:00-04:00   -0.754168</span>
<span class="go">2012-03-18 20:00:00-04:00   -1.434246</span>
<span class="go">2012-03-19 20:00:00-04:00    0.848935</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-700">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-701">警惕图书馆之间的转换。</span><span class="yiyi-st" id="yiyi-702">对于某些区域<code class="docutils literal"><span class="pre">pytz</span></code>和<code class="docutils literal"><span class="pre">dateutil</span></code>有不同的区域定义。</span><span class="yiyi-st" id="yiyi-703">对于不同的时区，这比“标准”区域（例如<code class="docutils literal"><span class="pre">US/Eastern</span></code>）更容易出现问题。</span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-704">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-705">请注意，时区库版本之间的时区定义可能不被视为相等。</span><span class="yiyi-st" id="yiyi-706">当使用使用一个版本进行本地化并使用不同版本进行操作的存储数据时，这可能会导致问题。</span><span class="yiyi-st" id="yiyi-707">有关如何处理此类情况，请参见<a class="reference internal" href="io.html#io-hdf5-notes"><span class="std std-ref">here</span></a>。</span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-708">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-709">It is incorrect to pass a timezone directly into the <code class="docutils literal"><span class="pre">datetime.datetime</span></code> constructor (e.g., <code class="docutils literal"><span class="pre">datetime.datetime(2011,</span> <span class="pre">1,</span> <span class="pre">1,</span> <span class="pre">tz=timezone(&apos;US/Eastern&apos;))</span></code>. </span><span class="yiyi-st" id="yiyi-710">相反，datetime需要使用localzone方法对时区进行本地化。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-711">在引擎盖下，所有时间戳都以UTC存储。</span><span class="yiyi-st" id="yiyi-712">来自具有时区的<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>的标量值将使其字段（日，小时，分钟）本地化到时区。</span><span class="yiyi-st" id="yiyi-713">但是，具有相同UTC值的时间戳仍被视为相等，即使它们在不同的时区：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [358]: </span><span class="n">rng_eastern</span> <span class="o">=</span> <span class="n">rng_utc</span><span class="o">.</span><span class="n">tz_convert</span><span class="p">(</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">)</span>

<span class="gp">In [359]: </span><span class="n">rng_berlin</span> <span class="o">=</span> <span class="n">rng_utc</span><span class="o">.</span><span class="n">tz_convert</span><span class="p">(</span><span class="s1">&apos;Europe/Berlin&apos;</span><span class="p">)</span>

<span class="gp">In [360]: </span><span class="n">rng_eastern</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>
<span class="gr">Out[360]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-03-10 19:00:00-0500&apos;</span><span class="p">,</span> <span class="n">tz</span><span class="o">=</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [361]: </span><span class="n">rng_berlin</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>
<span class="gr">Out[361]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-03-11 01:00:00+0100&apos;</span><span class="p">,</span> <span class="n">tz</span><span class="o">=</span><span class="s1">&apos;Europe/Berlin&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [362]: </span><span class="n">rng_eastern</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="o">==</span> <span class="n">rng_berlin</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>
<span class="gr">Out[362]: </span><span class="bp">True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-714">Like <code class="docutils literal"><span class="pre">Series</span></code>, <code class="docutils literal"><span class="pre">DataFrame</span></code>, and <code class="docutils literal"><span class="pre">DatetimeIndex</span></code>, <code class="docutils literal"><span class="pre">Timestamp``s</span> <span class="pre">can</span> <span class="pre">be</span> <span class="pre">converted</span> <span class="pre">to</span> <span class="pre">other</span> <span class="pre">time</span> <span class="pre">zones</span> <span class="pre">using</span> <span class="pre">``tz_convert</span></code>:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [363]: </span><span class="n">rng_eastern</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>
<span class="gr">Out[363]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-03-10 19:00:00-0500&apos;</span><span class="p">,</span> <span class="n">tz</span><span class="o">=</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [364]: </span><span class="n">rng_berlin</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>
<span class="gr">Out[364]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-03-11 01:00:00+0100&apos;</span><span class="p">,</span> <span class="n">tz</span><span class="o">=</span><span class="s1">&apos;Europe/Berlin&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [365]: </span><span class="n">rng_eastern</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="o">.</span><span class="n">tz_convert</span><span class="p">(</span><span class="s1">&apos;Europe/Berlin&apos;</span><span class="p">)</span>
<span class="gr">Out[365]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-03-11 01:00:00+0100&apos;</span><span class="p">,</span> <span class="n">tz</span><span class="o">=</span><span class="s1">&apos;Europe/Berlin&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-715"><code class="docutils literal"><span class="pre">Timestamp</span></code>的功能与<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>和<code class="docutils literal"><span class="pre">Series</span></code>相同：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [366]: </span><span class="n">rng</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>
<span class="gr">Out[366]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-03-11 00:00:00&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [367]: </span><span class="n">rng</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="s1">&apos;Asia/Shanghai&apos;</span><span class="p">)</span>
<span class="gr">Out[367]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2012-03-11 00:00:00+0800&apos;</span><span class="p">,</span> <span class="n">tz</span><span class="o">=</span><span class="s1">&apos;Asia/Shanghai&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-716">在不同时区中的系列之间的操作将生成UTC系列，对齐UTC时间戳上的数据：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [368]: </span><span class="n">eastern</span> <span class="o">=</span> <span class="n">ts_utc</span><span class="o">.</span><span class="n">tz_convert</span><span class="p">(</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">)</span>

<span class="gp">In [369]: </span><span class="n">berlin</span> <span class="o">=</span> <span class="n">ts_utc</span><span class="o">.</span><span class="n">tz_convert</span><span class="p">(</span><span class="s1">&apos;Europe/Berlin&apos;</span><span class="p">)</span>

<span class="gp">In [370]: </span><span class="n">result</span> <span class="o">=</span> <span class="n">eastern</span> <span class="o">+</span> <span class="n">berlin</span>

<span class="gp">In [371]: </span><span class="n">result</span>
<span class="gr">Out[371]: </span>
<span class="go">2012-03-06 00:00:00+00:00    1.358269</span>
<span class="go">2012-03-07 00:00:00+00:00    0.691336</span>
<span class="go">2012-03-08 00:00:00+00:00   -2.287805</span>
<span class="go">2012-03-09 00:00:00+00:00    0.974174</span>
<span class="go">2012-03-10 00:00:00+00:00   -2.842146</span>
<span class="go">2012-03-11 00:00:00+00:00   -0.654926</span>
<span class="go">2012-03-12 00:00:00+00:00    0.339798</span>
<span class="go">2012-03-13 00:00:00+00:00    1.735136</span>
<span class="go">2012-03-14 00:00:00+00:00   -1.668245</span>
<span class="go">2012-03-15 00:00:00+00:00   -3.396988</span>
<span class="go">2012-03-16 00:00:00+00:00    1.949435</span>
<span class="go">2012-03-17 00:00:00+00:00    1.933541</span>
<span class="go">2012-03-18 00:00:00+00:00   -1.508335</span>
<span class="go">2012-03-19 00:00:00+00:00   -2.868493</span>
<span class="go">2012-03-20 00:00:00+00:00    1.697870</span>
<span class="go">Freq: D, dtype: float64</span>

<span class="gp">In [372]: </span><span class="n">result</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[372]: </span>
<span class="go">DatetimeIndex([&apos;2012-03-06&apos;, &apos;2012-03-07&apos;, &apos;2012-03-08&apos;, &apos;2012-03-09&apos;,</span>
<span class="go">               &apos;2012-03-10&apos;, &apos;2012-03-11&apos;, &apos;2012-03-12&apos;, &apos;2012-03-13&apos;,</span>
<span class="go">               &apos;2012-03-14&apos;, &apos;2012-03-15&apos;, &apos;2012-03-16&apos;, &apos;2012-03-17&apos;,</span>
<span class="go">               &apos;2012-03-18&apos;, &apos;2012-03-19&apos;, &apos;2012-03-20&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns, UTC]&apos;, freq=&apos;D&apos;)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-717">要从tz感知<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>中删除时区，请使用<code class="docutils literal"><span class="pre">tz_localize(None)</span></code>或<code class="docutils literal"><span class="pre">tz_convert(None)</span></code>。</span><span class="yiyi-st" id="yiyi-718"><code class="docutils literal"><span class="pre">tz_localize(None)</span></code>将删除保存本地时间表示的时区。</span><span class="yiyi-st" id="yiyi-719"><code class="docutils literal"><span class="pre">tz_convert(None)</span></code>将在转换为UTC时间后删除时区。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [373]: </span><span class="n">didx</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DatetimeIndex</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="s1">&apos;2014-08-01 09:00&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;H&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">tz</span><span class="o">=</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">)</span>

<span class="gp">In [374]: </span><span class="n">didx</span>
<span class="gr">Out[374]: </span>
<span class="go">DatetimeIndex([&apos;2014-08-01 09:00:00-04:00&apos;, &apos;2014-08-01 10:00:00-04:00&apos;,</span>
<span class="go">               &apos;2014-08-01 11:00:00-04:00&apos;, &apos;2014-08-01 12:00:00-04:00&apos;,</span>
<span class="go">               &apos;2014-08-01 13:00:00-04:00&apos;, &apos;2014-08-01 14:00:00-04:00&apos;,</span>
<span class="go">               &apos;2014-08-01 15:00:00-04:00&apos;, &apos;2014-08-01 16:00:00-04:00&apos;,</span>
<span class="go">               &apos;2014-08-01 17:00:00-04:00&apos;, &apos;2014-08-01 18:00:00-04:00&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns, US/Eastern]&apos;, freq=&apos;H&apos;)</span>

<span class="gp">In [375]: </span><span class="n">didx</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
<span class="gr">Out[375]: </span>
<span class="go">DatetimeIndex([&apos;2014-08-01 09:00:00&apos;, &apos;2014-08-01 10:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 11:00:00&apos;, &apos;2014-08-01 12:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 13:00:00&apos;, &apos;2014-08-01 14:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 15:00:00&apos;, &apos;2014-08-01 16:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 17:00:00&apos;, &apos;2014-08-01 18:00:00&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;H&apos;)</span>

<span class="gp">In [376]: </span><span class="n">didx</span><span class="o">.</span><span class="n">tz_convert</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
<span class="gr">Out[376]: </span>
<span class="go">DatetimeIndex([&apos;2014-08-01 13:00:00&apos;, &apos;2014-08-01 14:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 15:00:00&apos;, &apos;2014-08-01 16:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 17:00:00&apos;, &apos;2014-08-01 18:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 19:00:00&apos;, &apos;2014-08-01 20:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 21:00:00&apos;, &apos;2014-08-01 22:00:00&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;H&apos;)</span>

<span class="c"># tz_convert(None) is identical with tz_convert(&apos;UTC&apos;).tz_localize(None)</span>
<span class="gp">In [377]: </span><span class="n">didx</span><span class="o">.</span><span class="n">tz_convert</span><span class="p">(</span><span class="s1">&apos;UCT&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
<span class="gr">Out[377]: </span>
<span class="go">DatetimeIndex([&apos;2014-08-01 13:00:00&apos;, &apos;2014-08-01 14:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 15:00:00&apos;, &apos;2014-08-01 16:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 17:00:00&apos;, &apos;2014-08-01 18:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 19:00:00&apos;, &apos;2014-08-01 20:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 21:00:00&apos;, &apos;2014-08-01 22:00:00&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;H&apos;)</span>
</pre></div>
</div>
</div>
<div class="section" id="ambiguous-times-when-localizing">
<span id="timeseries-timezone-ambiguous"></span><h3><span class="yiyi-st" id="yiyi-720">Ambiguous Times when Localizing</span></h3>
<p><span class="yiyi-st" id="yiyi-721">在某些情况下，localize不能确定DST和非DST小时有重复时。</span><span class="yiyi-st" id="yiyi-722">这通常发生在读取简单地重复小时的文件或数据库记录时。</span><span class="yiyi-st" id="yiyi-723">在<code class="docutils literal"><span class="pre">tz_localize</span></code>中传递<code class="docutils literal"><span class="pre">ambiguous=&apos;infer&apos;</span></code>（<code class="docutils literal"><span class="pre">infer_dst</span></code>参数）将尝试确定正确的偏移量。</span><span class="yiyi-st" id="yiyi-724">下面的顶部示例将失败，因为它包含模糊的时间，底部将推断正确的偏移量。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [378]: </span><span class="n">rng_hourly</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;11/06/2011 00:00&apos;</span><span class="p">,</span> <span class="s1">&apos;11/06/2011 01:00&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                                <span class="s1">&apos;11/06/2011 01:00&apos;</span><span class="p">,</span> <span class="s1">&apos;11/06/2011 02:00&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                                <span class="s1">&apos;11/06/2011 03:00&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-725">这将失败，因为有模糊的时间</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [2]: </span><span class="n">rng_hourly</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">)</span>
<span class="go">AmbiguousTimeError: Cannot infer dst time from Timestamp(&apos;2011-11-06 01:00:00&apos;), try using the &apos;ambiguous&apos; argument</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-726">推断模糊的时间</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [379]: </span><span class="n">rng_hourly_eastern</span> <span class="o">=</span> <span class="n">rng_hourly</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">,</span> <span class="n">ambiguous</span><span class="o">=</span><span class="s1">&apos;infer&apos;</span><span class="p">)</span>

<span class="gp">In [380]: </span><span class="n">rng_hourly_eastern</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="gr">Out[380]: </span>
<span class="go">[Timestamp(&apos;2011-11-06 00:00:00-0400&apos;, tz=&apos;US/Eastern&apos;),</span>
<span class="go"> Timestamp(&apos;2011-11-06 01:00:00-0400&apos;, tz=&apos;US/Eastern&apos;),</span>
<span class="go"> Timestamp(&apos;2011-11-06 01:00:00-0500&apos;, tz=&apos;US/Eastern&apos;),</span>
<span class="go"> Timestamp(&apos;2011-11-06 02:00:00-0500&apos;, tz=&apos;US/Eastern&apos;),</span>
<span class="go"> Timestamp(&apos;2011-11-06 03:00:00-0500&apos;, tz=&apos;US/Eastern&apos;)]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-727">除了&apos;infer&apos;，还有几个其他的参数支持。</span><span class="yiyi-st" id="yiyi-728">传递bool或0s / 1的数组，其中True表示DST小时，False表示非DST小时，允许区分多个DST转换（例如，如果在数据库中有多个记录，每个记录都有自己的DST转换）。</span><span class="yiyi-st" id="yiyi-729">或者传递“NaT”将用非一次性值填充转换时间。</span><span class="yiyi-st" id="yiyi-730">这些方法在<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>构造函数以及<code class="docutils literal"><span class="pre">tz_localize</span></code>中提供。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [381]: </span><span class="n">rng_hourly_dst</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span>

<span class="gp">In [382]: </span><span class="n">rng_hourly</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">,</span> <span class="n">ambiguous</span><span class="o">=</span><span class="n">rng_hourly_dst</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="gr">Out[382]: </span>
<span class="go">[Timestamp(&apos;2011-11-06 00:00:00-0400&apos;, tz=&apos;US/Eastern&apos;),</span>
<span class="go"> Timestamp(&apos;2011-11-06 01:00:00-0400&apos;, tz=&apos;US/Eastern&apos;),</span>
<span class="go"> Timestamp(&apos;2011-11-06 01:00:00-0500&apos;, tz=&apos;US/Eastern&apos;),</span>
<span class="go"> Timestamp(&apos;2011-11-06 02:00:00-0500&apos;, tz=&apos;US/Eastern&apos;),</span>
<span class="go"> Timestamp(&apos;2011-11-06 03:00:00-0500&apos;, tz=&apos;US/Eastern&apos;)]</span>

<span class="gp">In [383]: </span><span class="n">rng_hourly</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">,</span> <span class="n">ambiguous</span><span class="o">=</span><span class="s1">&apos;NaT&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="gr">Out[383]: </span>
<span class="go">[Timestamp(&apos;2011-11-06 00:00:00-0400&apos;, tz=&apos;US/Eastern&apos;),</span>
<span class="go"> NaT,</span>
<span class="go"> NaT,</span>
<span class="go"> Timestamp(&apos;2011-11-06 02:00:00-0500&apos;, tz=&apos;US/Eastern&apos;),</span>
<span class="go"> Timestamp(&apos;2011-11-06 03:00:00-0500&apos;, tz=&apos;US/Eastern&apos;)]</span>

<span class="gp">In [384]: </span><span class="n">didx</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DatetimeIndex</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="s1">&apos;2014-08-01 09:00&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;H&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">tz</span><span class="o">=</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">)</span>

<span class="gp">In [385]: </span><span class="n">didx</span>
<span class="gr">Out[385]: </span>
<span class="go">DatetimeIndex([&apos;2014-08-01 09:00:00-04:00&apos;, &apos;2014-08-01 10:00:00-04:00&apos;,</span>
<span class="go">               &apos;2014-08-01 11:00:00-04:00&apos;, &apos;2014-08-01 12:00:00-04:00&apos;,</span>
<span class="go">               &apos;2014-08-01 13:00:00-04:00&apos;, &apos;2014-08-01 14:00:00-04:00&apos;,</span>
<span class="go">               &apos;2014-08-01 15:00:00-04:00&apos;, &apos;2014-08-01 16:00:00-04:00&apos;,</span>
<span class="go">               &apos;2014-08-01 17:00:00-04:00&apos;, &apos;2014-08-01 18:00:00-04:00&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns, US/Eastern]&apos;, freq=&apos;H&apos;)</span>

<span class="gp">In [386]: </span><span class="n">didx</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
<span class="gr">Out[386]: </span>
<span class="go">DatetimeIndex([&apos;2014-08-01 09:00:00&apos;, &apos;2014-08-01 10:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 11:00:00&apos;, &apos;2014-08-01 12:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 13:00:00&apos;, &apos;2014-08-01 14:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 15:00:00&apos;, &apos;2014-08-01 16:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 17:00:00&apos;, &apos;2014-08-01 18:00:00&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;H&apos;)</span>

<span class="gp">In [387]: </span><span class="n">didx</span><span class="o">.</span><span class="n">tz_convert</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
<span class="gr">Out[387]: </span>
<span class="go">DatetimeIndex([&apos;2014-08-01 13:00:00&apos;, &apos;2014-08-01 14:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 15:00:00&apos;, &apos;2014-08-01 16:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 17:00:00&apos;, &apos;2014-08-01 18:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 19:00:00&apos;, &apos;2014-08-01 20:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 21:00:00&apos;, &apos;2014-08-01 22:00:00&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;H&apos;)</span>

<span class="c"># tz_convert(None) is identical with tz_convert(&apos;UTC&apos;).tz_localize(None)</span>
<span class="gp">In [388]: </span><span class="n">didx</span><span class="o">.</span><span class="n">tz_convert</span><span class="p">(</span><span class="s1">&apos;UCT&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
<span class="gr">Out[388]: </span>
<span class="go">DatetimeIndex([&apos;2014-08-01 13:00:00&apos;, &apos;2014-08-01 14:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 15:00:00&apos;, &apos;2014-08-01 16:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 17:00:00&apos;, &apos;2014-08-01 18:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 19:00:00&apos;, &apos;2014-08-01 20:00:00&apos;,</span>
<span class="go">               &apos;2014-08-01 21:00:00&apos;, &apos;2014-08-01 22:00:00&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=&apos;H&apos;)</span>
</pre></div>
</div>
</div>
<div class="section" id="tz-aware-dtypes">
<span id="timeseries-timezone-series"></span><h3><span class="yiyi-st" id="yiyi-731">TZ aware Dtypes</span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-732"><span class="versionmodified">版本0.17.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-733">具有时区<strong>naive</strong>值的<code class="docutils literal"><span class="pre">Series/DatetimeIndex</span></code>由<code class="docutils literal"><span class="pre">datetime64[ns]</span></code>的dtype表示。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [389]: </span><span class="n">s_naive</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">,</span><span class="n">periods</span><span class="o">=</span><span class="mi">3</span><span class="p">))</span>

<span class="gp">In [390]: </span><span class="n">s_naive</span>
<span class="gr">Out[390]: </span>
<span class="go">0   2013-01-01</span>
<span class="go">1   2013-01-02</span>
<span class="go">2   2013-01-03</span>
<span class="go">dtype: datetime64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-734">具有时区<strong>感知</strong>值的<code class="docutils literal"><span class="pre">Series/DatetimeIndex</span></code>由<code class="docutils literal"><span class="pre">datetime64 [ns，</span> <span class="pre">tz]  t5 &gt;</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [391]: </span><span class="n">s_aware</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">,</span><span class="n">periods</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span><span class="n">tz</span><span class="o">=</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">))</span>

<span class="gp">In [392]: </span><span class="n">s_aware</span>
<span class="gr">Out[392]: </span>
<span class="go">0   2013-01-01 00:00:00-05:00</span>
<span class="go">1   2013-01-02 00:00:00-05:00</span>
<span class="go">2   2013-01-03 00:00:00-05:00</span>
<span class="go">dtype: datetime64[ns, US/Eastern]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-735">这两个<code class="docutils literal"><span class="pre">Series</span></code>都可以通过<code class="docutils literal"><span class="pre">.dt</span></code>访问器操作，参见<a class="reference internal" href="basics.html#basics-dt-accessors"><span class="std std-ref">here</span></a>。</span></p>
<p><span class="yiyi-st" id="yiyi-736">例如，将本地化邮票本地化并转换为感知时区。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [393]: </span><span class="n">s_naive</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="s1">&apos;UTC&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">tz_convert</span><span class="p">(</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">)</span>
<span class="gr">Out[393]: </span>
<span class="go">0   2012-12-31 19:00:00-05:00</span>
<span class="go">1   2013-01-01 19:00:00-05:00</span>
<span class="go">2   2013-01-02 19:00:00-05:00</span>
<span class="go">dtype: datetime64[ns, US/Eastern]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-737">此外，您还可以<code class="docutils literal"><span class="pre">.astype(...)</span></code>时区感知（和天真）。</span><span class="yiyi-st" id="yiyi-738">这个操作实际上是一个本地化AND转换一个朴素的邮票，并转换一个意识邮票。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># localize and convert a naive timezone</span>
<span class="gp">In [394]: </span><span class="n">s_naive</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;datetime64[ns, US/Eastern]&apos;</span><span class="p">)</span>
<span class="gr">Out[394]: </span>
<span class="go">0   2012-12-31 19:00:00-05:00</span>
<span class="go">1   2013-01-01 19:00:00-05:00</span>
<span class="go">2   2013-01-02 19:00:00-05:00</span>
<span class="go">dtype: datetime64[ns, US/Eastern]</span>

<span class="c"># make an aware tz naive</span>
<span class="gp">In [395]: </span><span class="n">s_aware</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">)</span>
<span class="gr">Out[395]: </span>
<span class="go">0   2013-01-01 05:00:00</span>
<span class="go">1   2013-01-02 05:00:00</span>
<span class="go">2   2013-01-03 05:00:00</span>
<span class="go">dtype: datetime64[ns]</span>

<span class="c"># convert to a new timezone</span>
<span class="gp">In [396]: </span><span class="n">s_aware</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;datetime64[ns, CET]&apos;</span><span class="p">)</span>
<span class="gr">Out[396]: </span>
<span class="go">0   2013-01-01 06:00:00+01:00</span>
<span class="go">1   2013-01-02 06:00:00+01:00</span>
<span class="go">2   2013-01-03 06:00:00+01:00</span>
<span class="go">dtype: datetime64[ns, CET]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-739">注意</span></p>
<p><span class="yiyi-st" id="yiyi-740">使用<code class="docutils literal"><span class="pre">Series</span></code>上的<code class="docutils literal"><span class="pre">.values</span></code>访问器，返回数据的numpy数组。</span><span class="yiyi-st" id="yiyi-741">这些值将转换为UTC，因为numpy当前不支持时区（即使在本地时区<em>打印</em>）！</span><span class="yiyi-st" id="yiyi-742">）。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [397]: </span><span class="n">s_naive</span><span class="o">.</span><span class="n">values</span>
<span class="gr">Out[397]: </span>
<span class="go">array([&apos;2013-01-01T00:00:00.000000000&apos;, &apos;2013-01-02T00:00:00.000000000&apos;,</span>
<span class="go">       &apos;2013-01-03T00:00:00.000000000&apos;], dtype=&apos;datetime64[ns]&apos;)</span>

<span class="gp">In [398]: </span><span class="n">s_aware</span><span class="o">.</span><span class="n">values</span>
<span class="gr">Out[398]: </span>
<span class="go">array([&apos;2013-01-01T05:00:00.000000000&apos;, &apos;2013-01-02T05:00:00.000000000&apos;,</span>
<span class="go">       &apos;2013-01-03T05:00:00.000000000&apos;], dtype=&apos;datetime64[ns]&apos;)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-743">还要注意，一旦转换为numpy数组，这些将丢失tz的时间。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [399]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">s_aware</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
<span class="gr">Out[399]: </span>
<span class="go">0   2013-01-01 05:00:00</span>
<span class="go">1   2013-01-02 05:00:00</span>
<span class="go">2   2013-01-03 05:00:00</span>
<span class="go">dtype: datetime64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-744">然而，这些可以容易地转换</span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [400]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">s_aware</span><span class="o">.</span><span class="n">values</span><span class="p">)</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="s1">&apos;UTC&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">tz_convert</span><span class="p">(</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">)</span>
<span class="gr">Out[400]: </span>
<span class="go">0   2013-01-01 00:00:00-05:00</span>
<span class="go">1   2013-01-02 00:00:00-05:00</span>
<span class="go">2   2013-01-03 00:00:00-05:00</span>
<span class="go">dtype: datetime64[ns, US/Eastern]</span>
</pre></div>
</div>
</div>
</div>
</div>
